Azure App Services
.NET Core Azure Azure App Services Azure Storage C# PaaS Visual Studio

How to Create Azure AppService Web Jobs with .NET Core

Welcome to today’s post.

In today’s post I will be showing how to create an Azure Web Job using the Web Jobs SDK v3 In Visual Studio 2019.

With the process I will explain below, I will show how to create a web job within an Azure App Service. A web job is quite a useful part of an application that allows us to divert processes that would normally run within the application context into a separate job. The job can then be maintained and managed separately from the web application within the App Service.

A web job is run by using a trigger function. In turn our trigger function fires when a particular event on a resource such as a storage account has a change to its data.

The following are the steps we will take to create and setup our Azure web job:

  1. Create .NET Core Console App.
  2. Install NuGet Packages Microsoft.Azure.Webjobs.Extensions (v4.0.1) and Microsoft.Extensions.Logging.Console (v3.1.11) and Microsoft.Azure.WebJobs.Extensions.Storage (v3.0.11)
  3. Configure WebJobs, Azure Storage Service and Logging by making the following calls:
  4. Calling the HostBuilder extension method ConfigureWebJobs().
  5. Calling the WebJobsBuilder extension methods AddAzureStorageCoreServices() and AddAzureStorage().
  6. Calling the LoggingBuilder extension method AddConsole().
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

class program
{
    static async Task Main()
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
    	b.AddAzureStorageCoreServices();
        });
        var host = builder.Build();
        using (host)
        {
    	await host.RunAsync();
        }
    }
}

Add the trigger function in a separate class file:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace BookLoan.WebJob
{
    public class Functions
    {
        public static void ProcessQueueMessage([QueueTrigger("[queue name]")] string message, ILogger logger)
        {
            logger.LogInformation(message);
        }
    }
}

When the storage queue contains a new message, the above function will be triggered, and the log file will be written with the message text added to the queue.

To be able to run the Web Job, we will need to make use of an existing Azure Storage account.

From the Cloud Explorer, expand [Your Subscription] | Storage Accounts. Select a storage account.

From the properties, obtain the primary connection string.

Create a new configuration file, appsettings.json, with the following key/value:

{
  "AzureWebJobsStorage": "{azure storage connection string}",
}

In appsettings.json properties, set property Copy to Output Directory to Copy if newer.

We can test the web jobs locally by running the console app within Visual Studio.

The console will output as shown waiting for the trigger:

info: Host.Startup[0]
      Job host started
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: C:\xxxx\xxx\xxx xxx\xxxx\xxxx\BookLoan.WebJob\bin\Debug\netcoreapp3.1\

Select and open the queue from the Cloud Explorer:

In the queue select add message.

Enter a new message and submit the dialog.

Check the console and the new message should appear:

If the message does not trigger, then check the message queue name attribute in the trigger function matches the queue you have in Azure storage.

That is all for today’s blog.

I hope you found this blog useful and informative.

Social media & sharing icons powered by UltimatelySocial