Write a WebJob in a Website

Many times, in our web projects, we require a process to run separately from the main thread of the website. We had a scenario once to host our ASP.NET website on Windows Azure and there are 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 WebJob Create 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 project Create 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

  1. Click File > New Project, and then in the New Project dialog box click Cloud > Azure WebJob (.NET Framework).


  2. 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

  1. Right-click the web project in Solution Explorer, and then click Add > New Azure WebJob Project.

    The Add Azure WebJob dialog box appears.
  1. 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
  1. static JobHost Host = null;  
  2. static void Main() {  
  3.  var config = new JobHostConfiguration();  
  4.  if (config.IsDevelopment) {  
  5.   config.UseDevelopmentSettings();  
  6.  }  
  7.  Host = new JobHost(config);  
  8.  //Place Your code here if we want to execute a specific method after the job is triggered like below.  
  9.  Host.RunAndBlock();  
  10. }  
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 

  1. In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
  2. Select WebJobs.
     
  3. In the WebJobs page, select Add.

  4. Use the Add WebJob settings as specified in the table.

Using the steps mentioned above, a WebJob will start running if it's a continuous trigger; otherwise, you will have to start it manually if it is 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.