Azure WebJobs - Can I use Async Methods?

ericb picture ericb · Apr 7, 2014 · Viewed 12.3k times · Source

I was wondering if the Azure WebJobs SDK can trigger async methods? Currently I have a method that looks like the following:

class Program
{
    static void Main(string[] args)
    {
        var host = new JobHost();
        host.RunAndBlock();
    }

    public static void ProcessStuff([QueueInput("myqueue")] string msg)
    {
        var tsk = ProcessStuffAsync(msg)
                  .ContinueWith(x => HandleAsyncError(x),
                      TaskContinuationOptions.OnlyOnFaulted);
        tsk.Wait();
    }

    public static async Task ProcessStuffAsync(string msg)
    {
        // do some async stuff and await it
    }

    // other methods ...
}

However, I was wondering if I could just have the JobHost know to call my async method instead? There's not a ton of documentation out there on trying to use async/await in WebJobs, and it would be really nice if I could.

I'm trying to run this locally to test ... but the WebJobs SDK doesn't support the local Storage Emulator...

UPDATE 4/7/2014: Victor's answer is correct, but I did want to show what you'll see from using async methods in a WebJob (they do work).

For a method in your WebJob that looks like the following:

public async static Task ProcessMessageAsync([QueueInput("testq2")] string message)
{
    await Task.Delay(50);

    Console.WriteLine("Processing Message Async...");
    Console.WriteLine(message);
}

You will see the following output in your WebJobs log:

running in pid: 6272
Timestamp:4:36:02 PM
Parameters bound. Invoking user function.
--------
Warning: This asynchronous method will be run synchronously.
Processing Message Async...
a test message
--------
Success

Answer

Victor Hurdugaci picture Victor Hurdugaci · Apr 7, 2014

Async is now supported. See the blog post here.

Unfortunately, the short answer to both questions is: not supported.

(slightly) longer answer:

WebJobs SDK does not support async methods. If you look in the execution log (on the dashboard) you will see a warning saying that async functions (functions that return Task or Task<>) are executed synchronously.

We don't support the local emulator. You have to use a real Storage Account when developing.