Many a times in our web projects we require a process to run seperate from the main thread of website. We had a scenario once to host our asp.net website on Windows Azure. Different ways to do this.
The following file types are supported:
- .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)
a- Write the code in global.asax startup method and run a timer. But that will run inside the main thread of website so it is not advisable to do that, as any sort of crash in either website or the timer thread will cause the pool to restart.
b- Write a Windows Service, this is a classic model if we have a Virtual machine in place, but if we have hosted our website as a Windows Azure Website, then this will not work.
c- If we host a Windows Azure website, which is a common scenario, Windows Azure provides an option to configure the webjob host from the control panel where we can assign different properties like Triggering [continous, manual] and scheduling.
The Windows Azure WebJobs SDK is a framework that simplifies the task of adding background processing to Windows Azure Web Sites.
Aside from the main project, create a new C# Console application.
First, we need to install “Azure WebJobs SDK”, we can add it through nuget module in our Visual Studio project.
To create a new WebJobs-enabled project, you can use the Console Application project template and enable WebJobs deployment as explained in the previous section. As an alternative, you can use the WebJobs new-project template:
- Use the WebJobs new-project template for an independent WebJobCreate a project and configure it to deploy by itself as a WebJob, with no link to a web project. Use this option when you want to run a WebJob in a web app by itself, with no web application running in the web app. You might want to do this in order to be able to scale your WebJob resources independently of your web application resources.
- Use the WebJobs new-project template for a WebJob linked to a web projectCreate a project that is configured to deploy automatically as a WebJob when a web project in the same solution is deployed. Use this option when you want to run your WebJob in the same web app in which you run the related web application.
Use the WebJobs new-project template for an independent WebJob
- Click File > New Project, and then in the New Project dialog box click Cloud > Azure WebJob (.NET Framework).
- Follow the directions shown earlier to make the Console Application project an independent WebJobs project.
Use the WebJobs new-project template for a WebJob linked to a web project
- Right-click the web project in Solution Explorer, and then click Add > New Azure WebJob Project.
The Add Azure WebJob dialog box appears.
- Complete the Add Azure WebJob dialog box, and then click OK.
Deploy a WebJobs project
A WebJobs project that you have linked to a web project deploys automatically with the web project. For information about web project deployment, see How-to guides > Deploy app in the left navigation.
To deploy a WebJobs project by itself, right-click the project in Solution Explorer and click Publish as Azure WebJob….
For an independent WebJob, the same Publish Web wizard that is used for web projects appears, but with fewer settings available to change.
After it is created, in Program.cs
static JobHost Host = null;
static void Main()
var config = new JobHostConfiguration();
Host = new JobHost(config);
//Place Your code here if we want to execute a specific method after the job is triggered like below.
After we build the project, create a release.
Now from the Windows Azure panel, we can configure it using steps below.
Create a continuous WebJob
- In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
- Select WebJobs.
- In the WebJobs page, select Add.
- Use the Add WebJob settings as specified in the table.
Using Steps above, Webjob will start running if its a continous trigger, other wise manually if its set as a manual trigger.
Azure WebJobs has made a potentially difficult and complex feature an easy and simple one to implement. It’s stable, scalable, and easy to modify as our needs change. If you haven’t checked out Azure WebJobs yet, you should, it can be used for a wide variety of needs.