Azure WebJob with .Net Core Console Application

In this tutorial we are going to see how to use .Net Core Console Application as an Azure WebJob. .Net Core Console application is different from traditional .Net framework Console Application in generating the project outputs. Traditional .Net framework Console App generates executable as project output, but .Net Core Console App will generate DLL (library). We have to do little extra work to make .Net Core Console Application work as Azure WebJob.

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

image9

Once Web App was created, we can create an .Net Core Console Application which will be running as WebJob in the newly created Web App. Lets start by creating a simple Console Application using Visual Studio 2017 and netcoreapp1.0 as framework.

NOTE: Updated this tutorial on 2/26/2017 with MSBuild based Dotnet SDK.

I am using following version of SDK which is based on MSBuild/CSProj (remember that previous versions of ASP.Net Core are based on Project.json/XProj.).

 image

Code was self explanatory, it takes a user input and generates a multiplication table (just to mock a long running task).

using System;

namespace CoreConsoleApp
{
    public 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---");
        }
    }
}

Build the solution in Release mode. Create the below batch (with name run.bat, we can have any name) file and place it in \bin\Release\netcoreapp1.0 (or netcoreapp1.1 folder if we are using ASP.Net Core 1.1)folder. Batch file is required for WebJob to execute the DLL. Azure WebJob cannot execute the MAIN method in DLL, so it needs a entry point. In below script %1 is for accepting an input.

ECHO OFF
dotnet CoreConsoleApp.dll %1

Create a Zip package of entire \bin\Release\netcoreapp1.0 folder.

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

image15

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.

image26

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=6.To run an Azure WebJob with multiple parameters, please check this tutorial.

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, Click on Logs option as shown below.

image

We will see list of all the runs of CustomJob1.

image

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

image

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

You may also like...