Tim Murphy's .NET Software Architecture Blog

March 2015 Entries

Creating Azure WebJobs With The WebJobs SDK

BusseWoods20140924 (29)_1

The move to the cloud of many applications means that we have to learn new approaches to solving old problems.  One common problem that most developers face at one point or another is background processing for web sites.  In the past the solution would have often been to build a Windows Service and install it on a machine.  Of course if you are using Azure Web Sites to host you application you do not have access to install services.  Depending on what you need to accomplish WebJobs are the solution.  This post will discuss the basics of creating and deploying an Azure WebJob.

First you need to determine what type of WebJob makes sense to you.  There are the ones that are triggered by Azure events and those that are scheduled.  The ones that are triggered revolve around queues and data put into Azure storage.  These are generally referred to as continuous.  Conversely, if you have code that needs to be performed but is not based on an Azure resource then it is scheduled similarly to how we schedule tasks on a Windows server.

The main difference between the way triggered and scheduled WebJobs are coded is based around using the JobHost class and the Functions class.  In the Main method of a triggered job the RunAndBlock method of the JobHost object is executed as shown below.  This allows the job to run continuously.  

static void Main()
{
    var host = new JobHost();
    host.RunAndBlock();
}

The actual work is done in a method of the Functions class.  The parameters of each method define the if the function is triggered by a queue entry, a new blob storage or other resource.  This example is actually the default that is generated by the project template.

public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
{
    log.WriteLine(message);
}

The easiest way to write a WebJob that will run under a schedule is to simply remove the code in the Main method and put in your standard .NET code to perform your tasks.  At the same time you can remove the Functions class since it will not be used.

Set connection strings in the app.config for dashboard and storage to the name of your storage account and the storage account secondary access key.  They should look like the following configuration snippet. 

<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=StorageAccountName;AccountKey=StorageAccountSecondaryAccessKey" />
<add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=StorageAccountName;AccountKey=StorageAccountSecondaryAccessKey" />

They can be found at either of these two locations depending on the version of the portal you are using.

image

image

There are two ways you can deploy your WebJob to your Azure WebSite.  The first is to use Visual Studio.  The other way is to zip up your bin directory for you executable and upload that through the Azure portal.

image

Ultimately WebJobs are pretty easy to create once you find the basic information around their development.  Learn them and make your scalable cloud solutions on Azure more robust.