gpt4 book ai didi

c# - 为什么我在 Azure Function 中使用 async/Task 时出错,但在 Console App 中使用时却没有错误?

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:18 25 4
gpt4 key购买 nike

这就是我在控制台应用程序中使用 async/Task 的方式:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CaseDurableFunctionConsole
{

class Program
{
static async Task<List<string>> RunOrchestrator()
{
Console.WriteLine("Hello World!");
Console.ReadLine();
var outputs = new List<string>();
return outputs;
}

static void Main(string[] args)
{
RunOrchestrator();
}
}
}

这很好用,这是输出:

enter image description here

(即使不使用 await,它也会显示正确的结果。)

但是当我在 Azure Functions 中使用 async/Task 时,就像这样:

[FunctionName("Function1")]
public static async Task<List<string>> RunOrchestrator(
[OrchestrationClient] DurableOrchestrationClient orchestrationClient)
{
var outputs = new List<string>();
await Task.Delay(1);
return outputs;
}

然后启动后报错

[2019/11/25 5:50:50] Error indexing method 'Function1' [2019/11/255:50:50] Microsoft.Azure.WebJobs.Host: Error indexing method'Function1'. Microsoft.Azure.WebJobs.Host: Functions must return Taskor void, have a binding attribute for the return value, or betriggered by a binding that natively supports return values.

[2019/11/25 5:50:56] The 'Function1' function is in error:Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'.Microsoft.Azure.WebJobs.Host: Functions must return Task or void, havea binding attribute for the return value, or be triggered by a bindingthat natively supports return values.

如何通过修改我的功能代码来解决这个问题?

最佳答案

这与async-await无关.

日志消息指出,对于该绑定(bind),该方法应返回 Taskvoid并且您的方法返回 Task<List<string>> .

您的函数方法不等于 RunOrchestrator控制台应用程序中的方法。它等于 Main方法。

关于c# - 为什么我在 Azure Function 中使用 async/Task 时出错,但在 Console App 中使用时却没有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com