Learn to use .NET 9 WebJobs on App Service Linux.
WebJobs Intro
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. All app service plans support WebJobs. There's no extra cost to use WebJobs. This sample uses a Triggered (scheduled) WebJob to output the system time once every 15 minutes.
Create Web App
Before creating our WebJobs, we need to create an App Service webapp. If you already have an App Service Web App, skip to the next step Otherwise, in the portal, select App Services > Create > Web App. After following the create instructions and the .NET 9 runtime stack, create your App Service Web App. The stack must be .NET, since we plan on writing our WebJob using .NET and a bash startup script. For this example, we’ll use .NET 9.
Next, we’ll add a basic WebJob to our app.
Create WebJob
Before we do anything else, let’s write our WebJob. This will execute every time our WebJob is triggered. WebJobs on App Service can be run on a Triggered (Scheduled) basis or Continuously. This example uses a Triggered WebJob.
For this example, we’ll need to compress two files into a zip archive that we’ll upload to our App Service Web App. We’ll need a startup script and a .NET 9 dll, but since we’re running .NET on linux, we’ll need some additional dependencies as well. This tutorial assumes you are using a linux / WSL environment for development and building this application.
Create .NET Application
First, let’s create our .NET 9 app with the following command, this assumes that the .NET 9 sdk is installed and available on a linux development environment.
dotnet console new -n webjob –framework net9.0
Next, let’s update webjob/Program.cs to the following code. This will write the current time to the console.
using System;
class Program
{
static void Main()
{
DateTimeOffset now = DateTimeOffset.Now;
Console.WriteLine("Current time with is: " + now.ToString("hh:mm:ss tt zzz"));
}
}
Testing the Application
To test the application locally, use the following command and confirm that the current time is output to the console. This command must be run from the root of the application (webjob/).
dotnet run
Building the Application
Now that we’ve confirmed our app works, let’s build it for App Service Linux. To build it, from the application root, run:
dotnet build --self-contained
Packaging the Application for Azure WebJobs on App Service Linux
To run this application as an App Service Linux WebJob, it must be packaged in a zip file with a bash script (startup script) to run the application. Looking first at the startup script, create a file called run.sh that contains the following code:
#!/bin/bash
dotnet webjob/bin/net9.0/webjob.dll
Now we can package all our source files, including everything included with the `--self-contained` option. To do this, zip all the files together as shown below.
zip webjob.zip run.sh webjob/bin/Debug/net9.0/*
The resulting zip will be uploaded to the Azure Portal.
Create WebJob in Portal
Start by entering your Web App overview page. Then, under Settings select WebJobs. Here we can create and manage our App Service WebJobs for this Web App. Click Add to create a new WebJob.
Now we can name our WebJob, upload our zip from the previous step, and choose our execution type. Under Type, select Triggered. Under CRON Expression, enter the following to trigger our WebJob once every 15 minutes.
0 */15 * * * *
Note: These are NCRONTAB expressions, not standard Linux CRONTAB expressions. An important distinction.
Now click Create WebJob to finish making our new WebJob. Let’s test it out now.
Run Manually or Scheduled
To manually test our WebJob, we can click the play button under Run. A status of Completed means that WebJob is finished.
Confirm Results in Logs
We can check the logs to confirm that the console print statements from our NodeJS file were output to the console.
While this is a basic example, WebJobs are a powerful and easy to use feature that have incredible utility for running scheduled (or continuous) actions in conjunction with your App Service Web Apps at no additional cost.
Learn more about WebJobs on App Service