0
votes

I have created test Azure function with queue trigger. When I run this function in local emulator it works. But when I deploy it on Azure, it looks like it's not working (messages are not being deleted from queue).

Function

[FunctionName("Function1")]
    public static void Run([QueueTrigger("azurefunc", Connection = "StorageConnectionString")]string myQueueItem, TraceWriter log)
    {
        log.Info($"C# Queue trigger function processed: {myQueueItem}");

        // PERFORMANCE TEST
        for (int i = 0; i < 10000; i++)
        {
            for (int j = 0; j < 9000; j++)
            {
                // Do nothing.
                // Or do something
                string myAwesomeTempString = "How are you";
                myAwesomeTempString += "?";
            }
        }

        try
        {
            var queueMessage = new CloudQueueMessage (myQueueItem);
            QueueApi.DeleteMessage (QueueApi.AzureFunctionStorageQueue, queueMessage);
        }
        catch
        {

        }
    }

local.settings.json

{"IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "AZURE_STORAGE_CONNECTION_STRING",
    "AzureWebJobsDashboard": "AZURE_STORAGE_CONNECTION_STRING",
    "StorageConnectionString": "AZURE_STORAGE_CONNECTION_STRING",
    "serviceBusAccount": "SERVICE_BUS_CONNECTION_STRING"
  }}

Part of QueueApi

/// <summary>
    /// Creates instance of Queue Storage
    /// </summary>
    /// <param name="queueName">Name of queue storage</param>
    /// <returns>The instance of queue storage</returns>
    private static CloudQueue CreateStorageQueue (string queueName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse ("AZURE_STORAGE_CONNECTION_STRING");
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient ();
        CloudQueue queue = queueClient.GetQueueReference (queueName);
        queue.CreateIfNotExists ();
        return queue;
    }

    /// <summary>
    /// Deletes message from queue.
    /// </summary>
    /// <param name="queue">The queue storage instance</param>
    /// <param name="message">Message to be deleted</param>
    public static void DeleteMessage (CloudQueue queue, CloudQueueMessage message)
    {
        CheckInstances ();

        if (message != null)
        {
            queue.DeleteMessage (message);
        }
    }

I created Azure Service Bus account just to test another approach, but I would really rather use Azure Storage Queue.

Can anyone please help me?

Thank you very much

1

1 Answers

1
votes

Queue Trigger will delete the incoming message automatically at the end of function execution, hence you don't need to delete it on your own. Your code actually creates a new message which has same message content as the one comes into function, and tries to delete the new message which doesn't exist in the queue.

The reason the code doesn't work on Azure is that you may have forgotten to add StorageConnectionString in Application settings. local.settings.json is not published to Azure as it's only for local development.