How to create and deploy Webjobs in Azure

How to create and deploy Azure Webjobs

This Azure article will discuss how to create and deploy Webjobs in Azure.

How to create and deploy Azure Webjobs

Before discussing how to create and deploy Azure Webjobs, we will discuss What is Azure WebJobs? and the Prerequisites needed here before starting the actual functionality.

Webjobs in Azure are an excellent feature that allows you to run a script or program as a background process in your web app, mobile app, etc. You can also achieve the same functionality with the help of Azure Function.

Now, let’s discuss the prerequisites needed to start creating an Azure webjob.

Prerequisites

Below are the prerequisites needed for creating and deploying Azure Webjobs.

  1. You must have an Azure subscription or Azure Account. If you are new to Azure and don’t have an Azure account, you can create an Azure free account now.
  2. You must install Visual Studio 2019 or a later version on your machine. If you have not yet installed it, there is nothing to worry about. You can download and install Visual Studio 2019 now.

Assuming you are ready with all the prerequisites, let’s create a webjob in Azure using Visual Studio 2019.

Create webjob in Azure using Visual Studio 2019

Follow the below steps to create an Azure Webjobs using Visual Studio 2019.

  1. Open Visual Studio 2019 on your development machine.
  2. Click on the Create a new Project button.
  3. On the template window, search for Azure webjob and click on the search result Azure Webjob (.NET Framework) –> Click on the Next button.
webjobs in azure

4. On the Configure your new project window, provide the below details

  • Project name: Provide a valid name for your project.
  • Location: Choose a location where you want to save your project.
  • Framework: You can choose the latest version.

Finally, click on the Create button to create the project.

create webjob in azure

Now, you can see below that the project was created successfully.

how to create webjobs in azure

Open the Program.cs file, and you can see the below lines of code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;

namespace TsInfoWebjob
{
    // To learn more about Microsoft Azure WebJobs SDK, please see https://go.microsoft.com/fwlink/?LinkID=320976
    class Program
    {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        static void Main()
        {
            var config = new JobHostConfiguration();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }
    }
}

Below is the app. config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <!-- The format of the connection string is "DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY" -->
    <!-- For local execution, the value can be set either in this config file or through environment variables -->
    <add name="AzureWebJobsDashboard" connectionString="" />
    <add name="AzureWebJobsStorage" connectionString="" />
  </connectionStrings>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Make sure to add the connection strings for AzureWebJobsDashboard and AzureWebJobsStorage, as shown below. Then, change the Protocol, account name, and Account key based on yours.

<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY" />
    <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY" />

You will get the exception below if you don’t add the above connection strings in your app. config file.

System.InvalidOperationException: ‘Microsoft Azure WebJobs SDK Dashboard connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:

  1. Set the connection string named ‘AzureWebJobsDashboard’ in the connectionStrings section of the .config file in the following format or
  2. Set the environment variable named ‘AzureWebJobsDashboard’, or
  3. Set corresponding property of JobHostConfiguration.’

As the next step, Add or modify the code in the Program.cs file based on your requirements.

Now is the time to publish or deploy your Azure WebJobs to the Azure cloud.

How to deploy webjobs in Azure

Let’s discuss the quick steps to deploy your Azure Webjobs to the Azure cloud from Visual Studio 2019.

  1. Right-click on the project and select the Publish option as highlighted below.
how to deploy webjobs in azure

2. Now, you need to select a publish target, so select the Create New option to create a new app service. You can also select existing option to select an existing app service. Finally, click on the Create Profile button.

azure webjobs

3. Provide the below details on the Create New App service window.

  • Name: Provide a name for the App service.
  • Subscription: Choose a valid subscription that you want to use here.
  • Resource Group: Select an existing resource group you have, or you can use the default resource group created. You can also click the New button to create a new resource group.
  • Hosting Plan: Select a hosting plan or use the default hosting plan created.