Anders Martini
Anders Martini

Reputation: 948

how to add message to azure webjobs queue

I have a queued webjob performing a task using a queuetrigger as such:

    public static void ProcessQueueMessage([QueueTrigger("queue")],Data data, TextWriter log)
{
    //do stuff
}

In the same solution there is a website, what I need to do is to simply add messages to the queue from one of the websites controllers. Ive tried referensing the function directly, but it seems this simply runs the function instead of queuing the message, which is undesirable, since it creates a whole bunch of threads in a way that scales poorly.

Upvotes: 2

Views: 2108

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

The way QueueTrigger works is that it polls a queue (specified in the attribute which in your case is named "queue"). So basically for adding the message to this queue, you would simply reference Azure Storage Client Library in your website project, and add the message to the queue using code similar to below in your controller:

        var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        var queueClient = account.CreateCloudQueueClient();
        var queue = queueClient.GetQueueReference("queue");
        var msg = new CloudQueueMessage("message-contents");
        queue.AddMessage(msg);

You may also find this link helpful: https://azure.microsoft.com/en-in/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/

Upvotes: 6

Related Questions