Azure Web Job Implementation and Deployment 

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
queue.

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.a1

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 > Projectp2

ii. Under visual C#, select Azure Web Joba2

iii. Once solution gets added, it will looks as,a3

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

host.RunAndBlock();

In Function.cs, there is a function ProcessQueueMessage() which gets triggered when a new message is written in Azure message queue.a4In 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,a5

a6

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 Joba7

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,
a8

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.


4. Deploy Azure Web Job without using Visual Studio
i. Select Web Job in Solution explorer. Rebuild it in Release mode. Right click and select Open Folder in file explorer.

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 +Adda11

Fill details as,

For Continuous,a12

For Scheduled,a13

Select Ok. Web job should get added.

5. Check logs and debug Web job

Select web job. From top navigation select logs.
a14

a15

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 instancea16

  • ii. When a new message is added in Azure queue, ProcessQueueMessage function gets triggered.

a17

This way we can debug azure web job.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top