Azure WebJobs - Can I use Async Methods?

13,231

Solution 1

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.

Solution 2

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

Share:
13,231

Related videos on Youtube

ericb
Author by

ericb

Developer/Consultant at Codagami specializing in: .NET/C# ASP.NET MVC JavaScript (especially Knockout JS) ... and any other problems that need to be solved!

Updated on April 29, 2020

Comments

  • ericb
    ericb about 4 years

    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
    
    • Stephen Cleary
      Stephen Cleary about 10 years
      Why don't you try it and let us know?
    • ericb
      ericb about 10 years
      @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.
    • Michael Freidgeim
      Michael Freidgeim over 7 years
  • Stephen Cleary
    Stephen Cleary about 10 years
    That's "does not support async methods yet", right? ;)
  • ericb
    ericb about 10 years
    @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).
  • Victor Hurdugaci
    Victor Hurdugaci about 10 years
    @StephenCleary We are considering that feature for a future release.
  • Victor Hurdugaci
    Victor Hurdugaci about 10 years
    @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.
  • Ricardo Polo Jaramillo
    Ricardo Polo Jaramillo about 10 years
    @VictorHurdugaci in my code I have somethin like this public static void DeployToExchange([QueueInput(Constants.DeployToExchangeQueue‌​Name)] DeployToOwaCommand deploytoExchangeCommand) { System.Threading.Tasks.Task.Run(() => {} } and it looks like it works. Am I doing something wrong?
  • Victor Hurdugaci
    Victor Hurdugaci about 10 years
    @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.
  • Chris Foster
    Chris Foster over 9 years
    Here is a more official link: azure.microsoft.com/blog/2014/08/21/…