Run Your Background Jobs With Azure - WebJobs

Introduction

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 a virtual machine managed by Azure). You can run a worker role and other background processes there.
  2. Platform As A Service (PAAS) for an 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 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 personal table). I will create a simple Console Application and will deploy it as a web job.

Creating a Sample Application

 Sample Application

Solution Explorer

Create a simple Console application WebJob Sample

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

public void SendMail()
{
    List<Person> persons = _personRepository.GetAllPerson();
    foreach (Person person in persons)
    {
        //code to send the mail to all the person
    }
}

The following code is for the program. cs.

public class Program
{
    static void Main(string[] args)
    {
        SendMail();
    }

    static void SendMail()
    {
        PersonProcess process = new PersonProcess();
        process.SendMail();
    }
}

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 an Azure WebJob”.
     Azure WebJob
  2. There are 3 types of web job run modes as in the following.
     Run modes
    • There are many options enabled when you choose “Run on Schedule”. When you click “OK”, there is one screen to publish a job.
      OK
    • Here you choose to select a publish target, then you can choose any of your already created websites or you can create a new one.
       Web sites
    • Your Web Jobs will be published now. On the Azure Portal under the WebApps, you can see the newly published WebJob.
       Azure Portal

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

http://azure.microsoft.com/en-in/documentation/articles/web-sites-create-web-jobs/


Similar Articles