← back to the blog


Get Start with Azure WebJob

Posted by dake

How to schedule a Azure WebJob

Azure WebJob is back-end program you can run inside Azure, without Azure WebJob, you can deploy windows console app or windows service app to your server, then setup schedular via windows scheduler or other third-party windows scheduler tools. This blog will show you how to deploy Azure WebJob and how to scheduler. The Key technologies are following.

  1. Visual Studio 2015
  2. Azure
  3. About Azure WebJob
  4. Azure Scheduler Job Collections
  5. C#(6.0)
  6. ASP.NET Web Application

About following example, we'll use Visual Studio 2015's ASP.NET template to create ASP.NET web application, also we'll use visual studio 2015's Azure Web Job template to create Azure WebJob, then we'll deploy the ASP.NET web application and Web Job to Azure. At the end, we'll setup scheduler to the deployed Web Job at Azure.

Create Azure Account

First thing you have to do is create azure account, Azure has free trial period which give $200 for spend whithin first month. Once you create Azure account, you'll use the information for deployment.

Create an ASP.NET Web Application

You can create MVC or WEBAPI is fine, use ASP.NET Template to create web application.

Create WebJob

This is the key for this blog, we'll use Azure Cloud's Azure WebJob template to create app, 

Add  WebJob Web Application

At here, you'll add your webJob to the ASP.NET web application. The reason we do here is, when you deploy the web application, visual studio will also deploy the associate web job to Azure as well.

Following is the example at ASP.NET Web application, which specify which WebJobs' path.

{
  "$schema": "http://schemastore.org/schemas/json/webjobs-list.json",
  "WebJobs": [
    {
      "filePath": "../WebJob1/TN.PS.WebJob.csproj"
    }
  ]
}

Deploy Web Application

You can deploy web application by click the ASP.NET's publish and fill out all the information, or you could just deploy the webJob project if you want.

Verify and run WebJob

If have not setup scheduler but want to run the WebJob, you can either go to Azure, or use visual studio's Server Explorer to run the WebJob.

Following is example at Visaul Studio 2015's server exploer, you can just right click the WebJob1 and run it.

Following is at Azure Portal, under your web application find your webJob and right click the Run.

Call WebJob via Console App

You can also call the Azure Cloud job via C#, following is the example code of how to call the Azure Web Job, it is using basic authentication, however you don't have to provide that information for testing, but it is better for production for more secure web application.

    /// <summary>
        /// following run on top of C# 6.0
        /// also require following azure App Service information
        /// </summary>
        private static void Test()
        {
            try
            {
                //App Service Publish Profile Credentials 
                //userName 
                string userName = "";
                //userPWD 
                string userPassword = "";
                //change webJobName to your WebJob name 
                string webJobName = "";
                // change to your azure host name
                string azureHostName = "";

                var unEncodedString = string.Format($"{userName}:{userPassword}");
                var encodedString = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(unEncodedString));

                //Change this URL to your WebApp hosting the  
                string URL = "https://" + azureHostName + ".scm.azurewebsites.net/api/triggeredwebjobs/" + webJobName + "/run";
                System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
                request.Method = "POST";
                request.ContentLength = 0;
                request.Headers["Authorization"] = "Basic " + encodedString;
                using (System.Net.WebResponse response = request.GetResponse())
                {
                    using (System.IO.Stream dataStream = response.GetResponseStream())
                    {
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(dataStream))
                        {
                            string responseFromServer = reader.ReadToEnd();
                            Console.WriteLine("OK");
                        }
                    }
                }
                  
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

 

Setup a scheduler to the WebJob

You could use following Azure Scheduler Job Collections for create a scheduler.

Conclusion

Azure WebJob is good if you don't want to host virtual machine, but just want to deploy web app and back ground system.

Reference