Create a simple Azure WebJob with parameters

In this tutorial we are going to see how to create a simple Azure WebJob using a C# console application with parameters. Azure WebJobs are used to run scripts or programs (typically long running) as background processes on Azure Web Apps. WebJobs can be ran in three different ways – Manually triggered, Trigger on a Schedule and continuously trigger. WebJobs are capable of executing different scripts(for example cmd, bat, exe, js, jar, ps1 etc.,).

Lets get started by creating a new Azure Web App. I created my own custom service plan with B1 Pricing Tier and SouthEast Asia location (similarly we can create any other custom service plan). Also I am going to select an existing Resource Group (which I created long time back).

image

Once Web App was created, we can create an EXE which will be running as WebJob in the newly created Web App. Lets start by creating a simple Console Application using Visual Studio. For this tutorial, I am using VS 2015 with .Net 4.5 framework.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("---Start---");
        var value = int.Parse(args[0]);

        // Long task running
        for (var i = 0; i <= 1000; i++)
        {
            Console.WriteLine("{0} * {1} = {2}", value, i, (value * i));
        }

        Console.WriteLine("---Stop---");
    }
}

Code was self explanatory, it takes a user input and generates a multiplication table (just to mock a long running task). Build the solution in Release mode (For testing, even Debug mode also works fine). Create the Zip package of Release folder (which is created in Project’s Bin directory).

Now explore the Web App settings in Azure portal. Select WebJobs node.

image

Select Add and enter a Name for WebJob. Select Manual Trigger and finally upload the Zip package which we created previously.

image

Created WebJob will be listed as shown below.

image

Now we can trigger this WebJob using Postman tool. Remember to have the chrome browser interceptor for Postman tool, which will help in making the auth cookies available for Postman tool (make sure you are already logged into the Azure Portal in Chrome browser).

Using Postman tool, make a POST request to https://[Your Web App].scm.azurewebsites.net/api/triggeredwebjobs/CustomJob1/run?arguments=2.

image

We will get a 202 Accepted response. In above URL, arguments query string is used to pass the arguments to our console application.

To check the output of the WebJob, navigate to https://[Your Web App].scm.azurewebsites.net/azurejobs/#/jobs

image

Select CustomJob1, which will list all the runs of CustomJob1.

image

Click on any of the job run to see the generated output (which is a multiplication table of passed input).

image

2/24/2017 Update

To invoke a WebJob with multiple parameters, we need to follow below process. Lets say we have following Console Application code, where we need TWO arguments to perform computation.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("---Start---");
        var value1 = int.Parse(args[0]);
        var value2 = int.Parse(args[1]);
        // Long task running
        for (var i = 0; i <= 1000; i++)
        {
            Console.WriteLine("{0} * {1} = {2}", value1, value2, (value1 * value2));
        }

        Console.WriteLine("---Stop---");
    }
}

We should invoke the WebJob as shown below using POST.

https://[Your Web App].scm.azurewebsites.net/api/triggeredwebjobs/CustomJob1/run?arguments=2 3

image

Output of the WebJob is shown below.

image

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...