Azure WebJob 和实例异步方法

Azure WebJob and instance async method

我想在我的 Azure Web 作业中使用依赖注入。为此,我必须将 class 和方法设为非静态:

   public class Functions
    {
        [NoAutomaticTrigger]
        public async Task GetDriversAsync(TextWriter logger)
        {

        }

        [NoAutomaticTrigger]
        public void Test()
        {
        }
    }

然后我想将它传递给 JobHost 对象的 Call 方法。

我尝试这样做:

class Program
{
    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var host = new JobHost(config);

        Functions f = new Functions();

        host.Call(((Action)f.Test).Method);
        host.Call(((Action)f.GetDriversAsync).Method);
    }
}

使用同步方法它工作正常,尝试这样做 ((Action)f.GetDriversAsync).Method 我得到一个编译错误:

Cannot convert type 'method' to 'Action'

那一行怎么写才正确?

使用普通的同步方法。 如果查看 source code,您会发现宿主异步调用这些方法。或者你可以调用CallAsync等待任务

如果我找到了正确的 executor,您就不必担心您的方法会以 async/await 的方式被调用。

所以不要将您的方法转换为 Action。相反,

host.Call(typeof(Functions).GetMethod("GetDriversAsync"));