Most of the websites always require a batch processing job which can do resource intensive tasks like sending emails, listen to a queue, etc. Azure Webjobs
allows us to do just that and it's just a .NET console application which runs within the context of an Azure website.
These jobs can be scheduled to run on demand, continuously, or on a predefined schedule with a recurrence.
Additionally, operations defined within WebJobs
can be triggered to run either when the job runs or when a new file is created in Blob storage or a message is sent to an Azure queue.
To create an Azure webjob with queue trigger:
- Go to Visual Studio -> Right click your website -> Add -> New Azure
Webjob
project
- Open Program.cs.
- Modify the class
Program
so that it is public
. If you do not make the class public
, WebJobs
will not detect and run your operations. - Inside Program.cs, add a
using
statement for Microsoft.Azure.WebJobs
. - Inside
main()
, you will see these 2 lines of code:
JobHost host = new JobHost();
host.RunAndBlock();
which specifies that webjob
will run continuously.
- Next, add a method to be invoked by WebJobs, the following method will be invoked when a new message appears in the queue named
myqueue
.
public static void TriggerFunction([QueueTrigger("myqueue")]CloudQueueMessage message)
{
}
A webjob
can also be triggered when a new blob is detected within the container.
[BlobTrigger("input/{blobName}")]
To run this WebJob
locally, you need to apply some local configuration. Open the App.config file and add connection strings for the canonical storage account connection strings expected by WebJobs
SDK, AzureWebJobsDashboard
, and AzureWebJobsStorage
.
Be sure to replace name and key values with the values from your own storage account.
To test your webjob
, set it as a startup project and press f5. Your webjob
is up and running and waiting for a trigger, once a new message arrives in the queue the trigger function is triggered.
The post Azure webjobs with queue trigger appeared first on Tech Musings.