How To Develop And Deploy Azure WebJobs In Azure App Service

What is Azure WebJobs?

Azure WebJob is a feature of Azure App Service which runs a program or script in the background using a WebApp or WebAPI context. There is no additional cost to use WebJob. There are two types of web jobs.
  1. Continuous WebJob.
  2. Triggered WebJob.

Continuous WebJob

  • It starts immediately when Web Jobs is created.
  • It runs in all instances of the Web App. You can optionally restrict the Web Jobs to a single instance
  • It supports remote debugging.

Triggered WebJob

  • It starts manually or triggered.
  • It runs on a single instance for load balancing.
  • It does not support remote debugging.

Developing Azure WebJob using Visual Studio

Here we will see how to develop an Azure WebJobs using Visual Studio.
  • Open Visual Studio 2019, Click Create a new project.
  • Type Azure WebJob in the search box, choose Azure WebJob (.NET Framework) template and click Next.
Give appropriate Project name and Solution name, choose the location and select the Framework. Then click create.
This template creates a simple console application containing program.cs with Main Method and Function.cs file.
The WebJobs SDK looks for Azure Storage connection string in the app.config file. So give the Azure Storage connection string like below.
Let’s create a manual trigger using NoAutomaticTrigger attribute in Function.cs file.
  1. [NoAutomaticTrigger]  
  2. public void TriggerHandler(TextWriter log)  
  3. {  
  4.     try  
  5.     {  
  6.         log.WriteLine("Trigger is Fired");  
  8.     }  
  9.     catch (Exception ex)  
  10.     {  
  11.         log.WriteLine("Exception in Trigger Handler", ex);  
  12.         throw ex;  
  13.     }  
  14. }  
In Program.cs file contains the main method, call the manual trigger using JobHost function like below.
  1. class Program  
  2. {  
  3.     // Please set the following connection strings in app.config for this WebJob to run:  
  4.     // AzureWebJobsDashboard and AzureWebJobsStorage  
  5.     static void Main()  
  6.     {  
  7.         var config = new JobHostConfiguration();  
  9.         if (config.IsDevelopment)  
  10.         {  
  11.             config.UseDevelopmentSettings();  
  12.         }  
  14.         var host = new JobHost(config);  
  15.         host.Call(typeof(Functions).GetMethod("TriggerHandler"));  
  16.     }  
  17. }  

Deploy Azure WebJob In Azure App service

  • Build the solution in Release mode.
  • Go to bin/Release/net45 path of the application and add all the contents in a .zip file.
  • Go to the Azure portal, select your App service then go to WebJobs and ADD a Job. 
  • Enter the Job name, select the .zip file. Choose the type triggered. We can choose the Triggers by scheduled or Manual.
  • The scheduled WebJob will be executed based on the provided CRON expression.
  • Enter the CRON Expression and click Ok.
  • It will take a few minutes to add the Web Job.
  • Then select the WebJob you uploaded and click Run button.
  • You can see the status changed to Running, click Logs to see the WebJobs details like below. 
That’s it, I hope you have learned how to develop and deploy an Azure WebJobs in Azure App service. Feel free to fill up the comment box below if you need any assistance. 

Recommended Ebook

Understanding Azure : A Beginner’s Guide

Download Now!
Similar Articles