The Windows Azure Web Jobs SDK is a framework that simplifies the task of adding background processing to web sites. You can get more details about it by reading the Getting Started guide or Scott Hanselman’s introduction. Scott describes the Web Jobs SDK very well: “minimal ceremony for maximum results”. The Windows Azure Web Jobs SDK (codename SimpleBatch) is a very simple framework but provides some powerful binding capabilities.

The most simple advanced binding is the blob name placeholder. It allows you to express the intent of listening to blobs in a specific container that match a specific pattern. For example, you are only interested in blobs that end with “.jpeg”, or only blobs that are prefixed with “input-” and have the extension “.txt”, or you just want the name of the blob and you do not want to bind to an Azure SDK type.

public static void Thumbnails(
    [BlobInput("images/{name}.jpeg")] Stream inputImage,
    [BlobOutput("thumbnails/{name}.jpeg")] Stream outputImage)
{
    // Code that reads the image from inputImage and writes it to outputImage
}

The function above will only be triggered by blobs in the “images” container that have the extension “.jpeg”. The value of {name} placeholder from the input will replace the {name} placeholder in the output. For example, if the input blob is “images/img123.jpeg” then {name} is “img123″ and the output image is “thumbnails/img123.jpg”.

The name placeholders can be also be used to read and write to and from the same container without triggering an infinite loop (the output retriggers the function).

public static void Thumbnails(
    [BlobInput("images/full_{name}.jpeg")] Stream inputImage,
    [BlobOutput("images/thumb_{name}.jpeg")] Stream outputImage)
{
    // Code that reads the image from inputImage and writes it to outputImage
}

The function above reads from the “images” container and writes back in the same container. However, it is only triggered by blobs suffixed with “_full.jpeg” and writes blobs that end with “_thumb.jpeg”. For example, if the input blob is “images/full_img123.jpeg”, then {name} is “img123″ and the output image is “images/thumb_img123.jpeg”.

The placeholder binding is not limited to blob output only. If you need the value of {name} in the code, you can get it by adding a parameter named “name” to your function.

public static void Thumbnails(
    [BlobInput("images/{name}_full.jpeg")] Stream inputImage,
    string name,
    [BlobOutput("images/{name}_thumb.jpeg")] Stream outputImage)
{
    // Code that reads the image from inputImage and writes it to outputImage
}

When the function above executes, the parameter “name” will have the value of the {name} placeholder. For example: if the input is “images/img123_full.jpg” then “name” has the value “img123″.

Similar binding capabilities are available for queue messages too. Assume the following queue message object:

class Order
{
    public string CustomerName {get;set;}
    public string OrderId {get;set;}
}

When a new order arrives, we are going to get the Order message from a queue and create a blob (an invoice) with the name equal to the order id. One approach is to use the IBinder interface to bind the output blob and specify the name that equals the order id. The better and simpler approach (SimpleBatch) is to bind the blob name placeholder to the queue message properties:

public static void ProcessOrder(
    [QueueInput("orders")] Order newOrder,
    [BlobOutput("invoices/{OrderId}")] TextWriter invoice)
{
    // Code that creates the invoice
}

The {OrderId} placeholder from the blob name gets its value from the OrderId property of the newOrder object. For example, newOrder is (JSON): {"CustomerName":"Victor","OrderId":"abc42"} then the output blob name is “invoices/abc42″. The placeholder is case-sensitive.

3 comments

  1. Lucian on April 10th, 2014 at 2:28 pm

    Hi,
    A side question. :)
    In a cloud service worker I can create a simple while loop which does something on a regular basis — in my case some sqldb processing.
    How can I simply achieve the same with WebJobs SDK?

  2. Victor on April 10th, 2014 at 7:18 pm

    Hi Lucian,

    Unfortunately, the WebJobs SDK does not support sqldb. For now, you can only bind to Azure Storage entities.

  3. Lucian on April 11th, 2014 at 10:52 am

    Yes I’ve figured that out :).
    But the question was actually how would I implement let’s say a continuous while loop? So far I’ve only seen samples with a trigger pattern.

    Thx

Leave a comment

Please write the comment in English!

Allowed HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>