Azure WebJobs-我可以使用异步方法吗? [英] Azure WebJobs - Can I use Async Methods?

查看:70
本文介绍了Azure WebJobs-我可以使用异步方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Azure WebJobs SDK是否可以触发异步方法?当前,我有一个类似于以下内容的方法:

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 ...
}

但是,我想知道是否可以让JobHost知道调用我的异步方法?关于尝试在WebJobs中使用async/await的文档并不多,如果可以的话,这真的很好.

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.

我正在尝试在本地运行它以进行测试...但是WebJobs SDK不支持本地存储仿真器...

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

2014年4月7日更新:Victor的答案是正确的,但我确实想展示一下您在WebJob中使用异步方法会看到的结果(它们确实起作用).

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).

对于WebJob中的方法,如下所示:

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);
}

您将在WebJobs日志中看到以下输出:

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

推荐答案

现在支持异步.请参阅博客文章

Async is now supported. See the blog post here.

不幸的是,这两个问题的简短答案是:不支持.

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

(略长)答案:

WebJobs SDK不支持异步方法.如果您查看执行日志(在仪表板上),则会看到一条警告,指出异步功能(返回TaskTask<>的功能)是同步执行的.

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.

这篇关于Azure WebJobs-我可以使用异步方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆