Author : MD TAREQ HASSAN | Updated : 2020/11/16

What is WebJob?

In their simplest form, Azure WebJobs allow you to write background code that executes in the context of your web application or website. That means they’re not dependent on user interaction, but they are able to access the file system and other things that are available to your website.

Additional Links:

Why to use WebJobs

WebJob types

Webjobs Overview in Azure portal

Webjobs Overview in Azure portal

Activate app service always on setting

Activate app service always on setting

Creating WebJob project in Visual Studio

Functions.cs

using Microsoft.Extensions.Logging;

namespace WebJobDemo
{
    public class Functions
    {
        public static void Hover()
        {
            ILogger logger = new LoggerFactory().CreateLogger<Functions>();

            logger.LogInformation("Foo Bar Baz");
        }
    }
}

Program.cs

using System;

namespace WebJobDemo
{
    class Program
    {
        static void Main(string[] args)
        {
           Functions.Hover();
        }
    }
}

settings.job (every minute)

{
  "schedule": "0 */1 * * * *"
}

Creating WebJob project in Visual Studio Step 1

Publishing WebJob from Visual Studio

Publishing WebJob from Visual Studio Step 1

To change WebJob type

Change WebJob type when publishing from Visual Studio

Azure DevOps Pipeline

See: Azure DevOps Pipeline for Azure WebJobs

Utilizing WebJob SDK

Program.cs

using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebJobDemo
{
    class Program
    {
        static async Task Main()
        {
            var builder = new HostBuilder();

            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                // Starting with version 3.x, you must explicitly install the Storage binding extension required by the WebJobs SDK. 
                // In prior versions, the Storage bindings were included in the SDK.
                b.AddAzureStorage();
            });

            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            });

            var host = builder.Build();
            using (host)
            {
                await host.RunAsync();
            }
        }
    }
}

Functions.cs

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

namespace WebJobsSDKSample
{
    public class Functions
    {
        public static void ProcessQueueMessage([QueueTrigger("Xyz")] string message, ILogger logger)
        {
            logger.LogInformation(message);
        }
    }
}