Azure web jobs are implemented when it is a need to perform task in the background continuously or on scheduled basis. They are pretty similar to on premise timer jobs and event receivers. The Azure jobs run on the same
virtual machine where your web app is running on and support a variety
of file formats like exe, cmd, bat, sh, php, py and js.
Azure web job gets triggered when it detects entry in Azure message queue.
So before proceeding with this, entry should be added in Azure Message
1. Add entry in Azure Queue
Add entry in Azure queue before azure web job gets triggered. Following is the way to add entry in Azure queue.
i. UserDetailsEntity is a class with some predefined properties.This will be needed to pass values to azure web job. According to requirement this can be modified.
ii. StorageConnectionString is the key for connecting to azure storage account. E.g.
<add name=”StorageConnectionString ” connectionString=”DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY”/>
Find storage account name and key from portal.azure.com under web application.
iii. StorageQueueName is the queue name you want to get created for queuing requests. This can be anything according to the requirement.
2. Create Azure Web Job through Visual Studio
Open Visual Studio 2013 as Administrator
i. Navigate to file > New > Project
ii. Under visual C#, select Azure Web Job
iii. Once solution gets added, it will looks as,
This will have two main files, Program.cs and Function.cs.
Program.cs file consists of main function which gets executed when web job gets run.
// The following code ensures that the WebJob will be running continuously
In Function.cs, there is a function ProcessQueueMessage() which gets triggered when a new message is written in Azure message queue.In this method, first parameter “queue” is a queue name used while adding entry in Azure queue.
String message is a variable passed while adding entry in Azure queue, this can be any type of parameter like int, object, entity, Idictionary etc. used.
The main logic resides under process queue method.
iv. Edit App.config file’s AzureWebJobsDashboard & AzureWebJobsStorage values,
Copy these values from storage account by navigating to portal.azure.com > storage accounts > Access Keys
3. Deploying Web job on Azure using Visual Studio
i. Select web job. Right click > Click Publish as Azure Web Job
ii. Select WebJob run mode as per requirement. Click OK.
Note that, if you are selecting Run Continuously mode, your web app should be in the always on mode in Application setting.
Following window will appear,
iii. Select Import > Browse Publishing profile of Web app where web job
is to be hosted. Publishing profile can be taken from portal.azure.com >
App services > Select web app > Get Publish Profile
iv. Once profile is imported, validate the connection. Click Publish.
v. Web job should gets published on Azure Web App. This can be verified by
navigating to Portal.azure.com > App Services > Click Web App >
Under Web jobs (In right navigation) > Deployed web job should appear.
ii. Navigate to bin folder. Send Release folder to Zip file.
iii. Now navigate to portal.azure.com > app services > select web app
where job is to be hosted
iv. Under web jobs > select +Add
Fill details as,
Select Ok. Web job should get added.
5. Check logs and debug Web job
Select web job. From top navigation select logs.
In code, use Console.Out.Writeline(“Message”) to log information and to log error, use Console.Error.Writeline(“Message”)
To debug in Visual Studio,
i. Right click on job, select debug > Start new instance
- ii. When a new message is added in Azure queue, ProcessQueueMessage function gets triggered.
This way we can debug azure web job.