Author : MD TAREQ HASSAN | Updated : 2020/11/16
What is WebJob?
- WebJobs is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app, API app, or mobile app
- WebJobs are not seperate App service (can be hosted along with WebApps or independently)
- WebJobs allow to run backgroud tasks on underlaying servers (of App Service)
- WebJobs are similar to windows service running in Cloud
- WebJobs to simplifie many programming tasks
- Azure WebJobs SDK is with to develop WebJobs
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:
- https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started
- https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to
- https://github.com/Azure/azure-webjobs-sdk/wiki
Why to use WebJobs
- Part of app service (shares same compute service of web app, functions etc.)
- Reliable (same SLA of app services)
- Can be triggered by outside sources i.e. Queue, Blob, Table, SQL, WebHooks… OR
- Can be triggered manually or scheduled
- Can process triggers in parallel - multiple instances of same webjobs can process multiple task in parallel
- Background Service-as-a-Service
- You can deploy a batch job to be a webjob
WebJob types
- Triggered (default): A triggered WebJob starts based on a binding event, on a schedule, or when you trigger it manually (on demand). It runs on a single instance that the web app runs on
- Continuous: A continuous WebJob starts immediately when the WebJob is created. It runs on all web app scaled instances by default but can be configured to run as a single instance via
settings.job
Webjobs Overview in Azure portal
Activate app service always on setting
Creating WebJob project in Visual Studio
- Visual Studio provides project template for .Net framework, not for .Net core / .Net 5 (as of December 2020)
- Create a console app (.Net core or .Net 5) and then install nuget packages (if needed i.e. using other Azure services)
Install-Package Microsoft.Azure.WebJobs.Extensions
- WebJobs SDK (to use other Azure services i.e. Azure storage)
- Azure storage:
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage
- Add
Functions.cs
(static void Xxx(...)
will be called from Main method) - Create
settings.job
file and use cron expressions- Cron expression example:
0 */1 * * * *
(Every minute) - Cron generator:
- Cron expression example:
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 * * * *"
}
Publishing WebJob from Visual Studio
To change WebJob type
Azure DevOps Pipeline
See: Azure DevOps Pipeline for Azure WebJobs
Utilizing WebJob SDK
- SDK provides bindings to services
- Simplifies job coding and configuration
- Nuget:
Install-Package Microsoft.Azure.WebJobs.Extensions
(will also install required packages)- Azure storage service:
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage
- Console logging:
Install-Package Microsoft.Extensions.Logging.Console
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);
}
}
}