Run Your Background Jobs With Azure - WebJobs


There are many ways to run programs on Azure, you can run using:

  1. Infra Structure As A service (IAAS), which is a virtual machine; another option is Cloud Applications (still virtual machine managed by Azure). You can run a worker role and other background processes there.
  2. Platform As A Service (PAAS) for a Azure WebSite. That's also not a good solution for a Background Process.

So Azure Webjobs evolve here to solve this problem. Azure websites support the Webjobs to solve these problems. And there is no additional cost to use webjobs.

Suppose we need to implement a background task to send the email with their photos to all the employees (that are stored in the person table). I will create a simple Console Application and will deploy it as a webjob.

Creating a Sample Application

Create a simple Console application WebJob Sample.

Then in Process I have written the following code for sending the email:

  1. public void SendMail()  
  2. {  
  3.    List<Person> persons = _personRepository.GetAllPerson();  
  4.    foreach (Person person in persons)  
  5.    {  
  6.       //code to send the mail to all the person  
  7.    }  

The following code is for the program.cs:

  1. public class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       SendMail();  
  6.    }  
  8.    static void SendMail()  
  9.    {  
  10.       PersonProcess process = new PersonProcess();  
  11.       process.SendMail();  
  12.    }  

Now the following is the procedure to deploy the console application as a WebJob.

  1. Right-click on Console Application and select the option “Publish as a Azure WebJob”.

  2. There are 3 types of web job run modes as in the following:

There are many options enabled when you choose “Run on Schedule”. When you Click “OK”, there is one screen to publish a job.

Here you choose to select a publish target, then you can choose any of your already created web sites or you can create a new one.

Your Web Jobs will be published now. On the Azure Portal under the WebApps, you can see the newly published WebJob.

Please have a look at the following document for a detailed understanding of WebJobs: