Hosting Azure webjobs outside Azure, with the logging benefits from an Azure hosted webjob

First there is the Azure Webjob SDK

A little after Azure Webjobs was first introduced, the AzureWeb job SDK was also introduced, making it possible to trigger a job by an “event” like a message in a queue, a new blob etc. This makes it a lot more frictionless and clean to make a job being triggered by some event, compared to implement the trigger yourself inside the job with with polling.

The basic job

First we need to create a regular console application using Visual Studio and install the nuget package Microsoft.WindowsAzure.Jobs.host , to enable the webjob sdk features. Please not that that this package is currently in prelease so the command to nuget should include the prerelease option:

install-package microsoft.windowsazure.jobs.host -Prerelease

With this package, we can create our worker method. For simplicity we create a simple method that gets triggered by a message in a queue and log the input.

public static void Capture([QueueInput("orderqueue")] string orderMessage)
{
     Console.WriteLine(orderMessage);
     //real work goes here.....
}

This tells the webjob SDK to look for a message in a queue named orderqueue in your storage account, so we also need to specify the storage account details.

static void Main(string[] args)
{
    var host = new JobHost("DefaultEndpointsProtocol=http;AccountName=YourAccountNameHere;AccountKey=YourAccountKeyHere");
    host.RunAndBlock();
}

Specify a new JobHost and start it inside the main method. You need to replace  [AccountName] and [AccountKey] with your own credentials. This will enable you to reference the orderqueue used in the worker method. All the wiring is done “automatically” by the webjob SDK.

Hosting the WebJob outside the Website webjobs

Under normal circumstances you would host your webjob as part of an azure website. The reason I want to make additional hosting outside the azure environment, is because I’m using a library in a another projec that’s referencing GDI, and GDI is not supported by Azure webjobs yet. So a great opportunity to show additional webjob hosting.

Deploy and start the webjob

The deployment is so simple, just copy your webjob to a VM or run it directly on your own machine. Start the job as you would with a regular console application.

Linking to an Azure website to enable logging

To enable logging for the webjob, we need to create an Azure website as we would for normal hosting. But the trick is that inside the Configure section we add a connectionstring for the AzureJobsRuntime. If you are only viewing logs as illustrated below by Visual Studio, this step can be omitted.

 

Logging from the VM to Azure Website

Finally it’s time to show it all in action. From within Visual Studio server explorer, create a new queue with the name we used earlier orderqueue.

serverexplorer_queue

Open the queue and “add new message”. We can do this as we are using a basic string for input, so just wite “Hello Log” and press ok, the message is now in the queue.

If not started, then start the webjob. The job will soon pick up the message you added to the queue, illustrated as this in the cnsole.

capture_message

To view the content of the message, open the Server Explorer and open the Blob node under your storage account.

storage_log

The log should now contain 2 entries. Click the largest one and you should now see your message content statement inside.

job_log

 

notepad_log_msg

So this is all it takes to enable logging on a webjob hosted outside an Azure website.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>