Microsoft Azure provides its Web App Service to execute background tasks as WebJobs. Using the WebJobs feature of Azure App Service, developers can run a program or script in the same instance as a web app. Developers can use Visual Studio 2019 to develop and deploy WebJobs in Azure, as we will demonstrate in this tutorial. WebJobs support:
- .cmd
- .bat
- .exe (using Windows cmd)
- .ps1 (using PowerShell)
- .sh (using Bash)
- .php (using PHP)
- .py (using Python)
- .js (using Node.js)
- .jar (using Java)
This article will demonstrate the deployment of a WebJob in Azure Web App service using Visual Studio 2019.
How to Create an App Service in Azure
To begin, go to Azure Portal and either search for an existing Resource group or create a Resource group if you do not have any already. Search for App service and create an app service. Refer to the figure below and be sure to provide all of the required details.
Next, select Publish code and .NET 5 as Runtime. Then go to the Monitoring tab if you want to enable Application Insights. By default, application insights will be enabled.
Click on Review + Create and then click on Create.
Once the resource is created, go to the Azure portal, then Resource groups, followed by Resource (web app service created), then webjob Appservice. Click on “Get Publish Profile” and Download. We will use the downloaded published project in the next steps and import it into Visual Studio to publish the web job.
Refer to the following figure for more:
How to Create a Webjob in Visual Studio
Open Visual Studio 2019 and navigate to File, New, Project and then select Azure WebJob (.NET Framework) project template as depicted in the figure below:
Next, add a project name and browse folder location to save the project. Then click ‘Create’ to add a new project.
Next, right click on solution, then click add, New Item, select JSON as the type and name it Settings.job. Refer to the figure below:
Then, open “Settings.job” and the following code for scheduling details of the Job:
{ "schedule": "0 0 */4 * * *" }
Next, add the following sample code in the Function.cs file of the WebJob project created in the previous step. See below:
Now we will deploy the WebJob in the Azure app service resource we created earlier. Right-click on the Solution Explorer and click Publish. Next, click ‘Start’. Refer to the image below:
Select Import Profile and click ‘Next’. See below:
Browser the path of previously exported published profile and click Finish. Check out the image below for more:
Finally, publish the WebJob by clicking the Publish button, as seen below:
Visual Studio will build the code and publish the WebJob in Web App service:
Now, go back to the Azure Portal and open the WebJobs Tab of the Web App Service. You will notice the sample WebJob has been deployed:
Deploying Webjobs with Azure
I hope this article will be helpful for developers starting out with Azure WebJobs. Microsoft has released version 3.x of the Azure WebJobs SDK. Now, developers can create and publish WebJobs as .NET Core console apps.