Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Azure webjobs with Queue Trigger

0.00/5 (No votes)
14 Jul 2015CPOL2 min read 21.1K  
Azure webjobs with queue trigger

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

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:

  1. Go to Visual Studio -> Right click your website -> Add -> New Azure Webjob project

    1

  2. Open Program.cs.
  3. 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.
  4. Inside Program.cs, add a using statement for Microsoft.Azure.WebJobs.
  5. Inside main(), you will see these 2 lines of code:
    C#
    JobHost host = new JobHost();
    host.RunAndBlock();

    which specifies that webjob will run continuously.

  6. 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.
    C#
    public static void TriggerFunction([QueueTrigger("myqueue")]CloudQueueMessage message)
    {
    }

    A webjob can also be triggered when a new blob is detected within the container.

    C#
    [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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)