Introduction

In this article, we're going to take a look at Web Jobs and help you understand what they are, how they work, and how they can benefit you. We'll get started by taking a look at exactly what a Web Job is. We're going to take a look at utilizing the WebJobs SDK to both simplify the code that we write and extend the reach of our Web Jobs code. Then we'll see what the WebJobs SDK provides in terms of bindings to services, things like Azure Queues, Service Bus, Table and Blob Storage, the other messaging or data-related services. It's going to simplify the job we have of coding and configuring our Web Jobs so that we don't write a lot of the boilerplate code to connect to and access these resources.

Road Map

  1. What is an Azure WebJob
  2. Azure WebJob vs Azure Functions
  3. Installing WebJob SDK in Visual Studio
  4. Creating a WebJob using Azure WebJob SDK
  5. Configuring a Queue in Azure Storage to send message
    • Create a new storage account
    • Create a queue and send messages
  6. Running the WebJob
  7. Sending a message to trigger the Job
  8. Working with Bindings and Triggers
  9. Use Cases

What is an Azure WebJob?

In their simplest form, Azure Web Jobs allows you to write background code that executes in the context of your web application or website. That means they're not dependent on user interaction, but they are able to access the file system and other things that are available on your website. There's lots of work that needs to happen behind the scenes when a user is not interacting with the site. And that's where Web Jobs come in. A Web Job could run in the background of your application, and it can then access things like your database, the files, either temporary files, log files, or source files, for your application, and because it's running in there, you can also have access to the various pieces of your application back end.

Azure WebJob vs Azure Functions

Azure Functions is a service that you can use to run code in Azure without provisioning any servers or other infrastructure. Because a Function can run on a schedule or continuously, you can use them instead of WebJobs to execute background tasks. You can think of Azure Functions as the logical successor to WebJobs for most workloads. In fact, Azure Functions is built on the WebJobs SDK.

For most automated tasks, build Functions because they are more flexible than WebJobs. For example, you can develop and test Functions entirely in the browser, and Functions can automatically scale in response to demand.

However, code deployed as a WebJob can be developed and maintained alongside the code for the website it is associated with. A website with its WebJobs can be easily packaged, deployed, and managed together, as a single solution. By contrast, Azure Functions are separate from the App Service and must be managed separately.

Installing Azure WebJobs SDK

Write the following command in the NuGet package manager console in your Visual Studio,

Install-Package Microsoft.Azure.WebJobs –Version 2.2.0

 Microsoft Azure

Creating a WebJob project

Now create a webjob project from Visual Studio as shown. Assume that we are writing a job just to print a message it gets.

  1. Create a new project and choose Azure WebJob
    Azure WebJobs
  2. Choose a name for it and press Create
    Press Create

If we look at the Program.cs file we will see a static void Main, and we have this config and the JobHost. These are all part of the WebJobs SDK. That JobHost is going to be responsible for running and blocking inside my console application, and then it's going to be hosting other functions.

The Code below is automatically generated by the project, you don’t have to write it.

Code

So if we go look at our functions.cs here, notice that we have a specific signature on this method, static void method, and then the parameters, but there's this attribute called QueueTrigger on the message.

The Code below is automatically generated by the project, you don’t have to write it.

Static void method

Again, this is part of that WebJobs SDK, and what that says is, go look at a queue, in this case, called queue, and when messages arrive, pull them off, pull the string out, and invoke this method, passing in the message. Now let’s create a messaging queue in the Azure portal to send messages. But first, we will have to create a storage account.

Configuring a Queue in Azure Storage to send message

For this, we will have to create a storage account in the Azure portal.

Running the WebJob

Now go back to the WebJob project and make some changes.

Sending Messages to trigger the WebJob

  1. Now go to the portal, and navigate to the queues.
  2. Click on Add Message
    Add Message
  3. Type anything a click OK
    Click OK
  4. Now go back to the console and you will see the new Message there.
    Message there

And that’s how you can use WebJob SDK to receive upcoming messages from the storage queue. Similarly instead of displaying a simple message you can do a heavy background task based on any trigger.

Working with Bindings and Triggers

We've seen a basic example of just reading a string of the queue and writing it out to the console, but let's take this a step further. I've created a User class here in my WebJobs project that just has a Name, an ID, and a Gender. And what I want to be able to do is get that particular user off the queue.

Similarly, instead of Blob storage, we can use tables to store our data.

Use Cases