Introduction
Azure Web Job is the most useful feature of Azure WebApp service. It is greatly helpful when you want to run the scripts or programs as background processes in-app services, API, app, and mobile app.
You can also upload and run an executable file such as cmd, bat, exe, ps 1, sh, php, py, etc. These programs all run as Web Jobs on a schedule (cron) or continuously.
The purpose of the web job is to simplify the code and also to perform common tasks, such as image processing, file maintenance, and sending mail. The web job SDK has a great built-in feature for working with Azure storage and Service bus. It is very helpful for many scenarios like scheduling tasks and handling errors etc.
By using Visual Studio, you can create, deploy, and manage Web Jobs. It’s seamless with integrating tooling in Visual Studio. It’s very easy to create Web Jobs from templates, publish, and manage (run, stop, monitor, and debug) them.
Let's see an example of how to create a Web job and host it on Azure.
- Open Visual Studio.
- Create New project -> File -> New - project
- Now select Template -> Cloud - Azure Web Job And give the appropriate name and location path where you want to create a project in your local directory.
- Click on the OK button.
- You will see the solution structure like this.
- You can use this web job for various purposes as I mentioned earlier like sending mail notifications etc.
- Now we will see how to upload this web job onto Azure.
- First, you need to build and run your solution for creating the exe or dll files.
- You can upload a zip of your solution into the web job.
- Go to Solution Explorer right-click on your project then click on an open folder in File Explorer.
- Now create a zip which you will upload on the web job.
- Now upload this zip file in the web job.
- Open the Azure portal and open your web app.
- Type Web job in Search Box.
- Then click on Web Jobs so the new blade will open as shown in the below image.
- Then Click on the ADD ‘+’ Sign.
- A New Window will open.
- Enter the name.
- Upload the file that you already zipped and Select type.
- Select Type – “Triggered or Continuous “according to your requirements.
- Let's say we select Triggered because we want to run our web job as scheduled.
- So next Select Triggers as “Scheduled or manual “.
- Let's Say We Select Scheduled.
- Give the CRON Expression.
- Referred https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer for a better understanding of CRON Expression.
- Let's say we give 0 */5 * * * * To the CRON Expression.
- We want to trigger our web job once every 5 minutes.
- Click on the OK button.
- You will see you like this.
Summary
In this article, we learned how to Create a Web Job in Azure Web apps. I hope you like it if you have any questions drop me a message.