Using Web Jobs In Azure App Services

Introduction

Today we will look into Web Jobs in Azure App Services. These are standalone applications that we can run within the context of an Azure Application service. These are useful for creating monitoring jobs etc. that will run independently of the main application. So, let us begin.

Creating the Web Job

We will be creating the Web Job in Visual Studio 2022 Community Preview edition using .NET 6 preview version. A Web Job is just a simple console application and will be created as below,

Using Web Jobs in Azure App Services

Using Web Jobs in Azure App Services

Using Web Jobs in Azure App Services

Using Web Jobs in Azure App Services

Using Web Jobs in Azure App Services

Update the code as below in the Program.cs file,

Console.WriteLine("Hello, Running as WebJob from Azure App Service!");

Next, run the application locally to ensure all works fine.

Using Web Jobs in Azure App Services

Let us run the created application from the command prompt as below,

Using Web Jobs in Azure App Services

We need to create a “Run.Cmd” file and add the below line to it,

dotnet AzureWebJob.dll

Ensure, this file is copied to the output and has a “UTF-8” encoding. This can be confirmed and set from Notepad++ as below,

Using Web Jobs in Azure App Services

We are now ready to publish this file in release mode. Open the Terminal as below,

Using Web Jobs in Azure App Services

And run the below command,

dotnet publish -c release

Using Web Jobs in Azure App Services

Finally, zip the publish folder as “Publish.zip” created in the output and you are ready to deploy the web job to Azure.

Deploying the Web Job

We will now deploy the web job we created to an Azure App service. I have already created an App service which is a web app as below,

Using Web Jobs in Azure App Services

Open the App Service in the Azure Portal and select “WebJobs” in the left side menu as below,

Using Web Jobs in Azure App Services

Add the WebJob as above and save.

Using Web Jobs in Azure App Services

Run the Web Job and you will get a message that it ran successfully. As the job simply outputs a line to the console, open the web jobs logs and you will see the below,

Using Web Jobs in Azure App Services

Hence, we see that our Web Job was executed.

Summary

In this article, we looked at creating a simple web job and deploying it in an Azure App service. The example was simple but explains the overall process we need to follow if we want to create such an artifact for our Azure App service solution. Happy coding!


Similar Articles