Introduction
There are many ways to run programs on Azure, you can run using:
- 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.
- 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:
- public void SendMail()
- {
- List<Person> persons = _personRepository.GetAllPerson();
- foreach (Person person in persons)
- {
-
- }
- }
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.
- Right-click on Console Application and select the option “Publish as a Azure WebJob”.
- 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:
http://azure.microsoft.com/en-in/documentation/articles/web-sites-create-web-jobs/