22

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
Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
ericb
  • 3,400
  • 1
  • 21
  • 21
  • Why don't you try it and let us know? – Stephen Cleary Apr 07 '14 at 15:40
  • 1
    @StephenCleary currently working on it - just wanted to avoid setting up all the azure storage/website accounts and configuration if someone else had already done it successfully. – ericb Apr 07 '14 at 15:44
  • 1
    Possible duplicate of [How to do Async in Azure WebJob function](http://stackoverflow.com/questions/38249919/how-to-do-async-in-azure-webjob-function) – Michael Freidgeim Nov 01 '16 at 12:18

2 Answers2

25

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.

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • 1
    That's "does not support async methods *yet*", right? ;) – Stephen Cleary Apr 07 '14 at 15:50
  • @VictorHurdugaci - I guess my goal here is that I just need to be able to use the async methods of my shared/3rd party libraries. If the JobHost processes them synchronously, thats fine (for now). – ericb Apr 07 '14 at 15:58
  • 2
    @StephenCleary We are considering that feature for a future release. – Victor Hurdugaci Apr 07 '14 at 16:05
  • @ericb Even in methods that don't return Task and/or are not marked as `async` you can use `Task<>.WaitForResult( .. )` to consume async methods in a synchronous way. That's what we do in WebJobs when we see an async function. – Victor Hurdugaci Apr 07 '14 at 16:07
  • @VictorHurdugaci in my code I have somethin like this public static void DeployToExchange([QueueInput(Constants.DeployToExchangeQueueName)] DeployToOwaCommand deploytoExchangeCommand) { System.Threading.Tasks.Task.Run(() => {} } and it looks like it works. Am I doing something wrong? – Ricardo Polo Jaramillo May 18 '14 at 22:47
  • @RicardoPolo If you do that without waiting for the task (the one created by Task.Run) to complete, there is a chance that the function will end before the task. In that case, you will only get logging up to the point where the function ran. If you don't care about the logs, then I guess it should be alright. – Victor Hurdugaci May 19 '14 at 01:52
7

With the passage of time, the answer is now, yes, you can!

Nate Jackson
  • 549
  • 4
  • 15
  • Here is a more official link: http://azure.microsoft.com/blog/2014/08/21/announcing-the-0-4-0-beta-preview-of-microsoft-azure-webjobs-sdk/ – Chris Foster Sep 15 '14 at 10:13