Azure webjobs

In this article, we are going to see how to develop and deploy Azure web Job 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 contains 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

      [NoAutomaticTrigger]

        public void TriggerHandler(TextWriter log)

        {

            try

            {

                log.WriteLine(“Trigger is Fired”);              

            }

            catch (Exception ex)

            {

                log.WriteLine(“Exception in Trigger Handler”, ex);

                throw ex;

            }

        }

  • In Program.cs file contains the main method, call the manual trigger using JobHost function like below

 class Program

    {

        static void Main()

        {

            var config = new JobHostConfiguration();   

            if (config.IsDevelopment)

            {

                config.UseDevelopmentSettings()

            }

            var host = new JobHost(config)

            host.Call(typeof(Functions).GetMethod(“TriggerHandler”));    

        }

    }

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.