在.NET5中将IActionResult与AzureFunctions一起使用?
c#
将我的 Azure Functions 项目迁移到 .NET 5 后,它开始将我的响应包装在一个奇怪的包装类中。
例如,考虑以下端点:
public record Response(string SomeValue);
[Function("Get")]
public async Task<IActionResult> Get(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get-something")]
HttpRequestData request)
{
return new OkObjectResult(new Response("hello world"));
}
之前,它会返回:
{
"someValue": "hello world"
}
但现在,它返回:
{
"Value": {
"SomeValue": "hello world"
},
"Formatters": [],
"ContentTypes": [],
"DeclaredType": null,
"StatusCode": 200
}
我知道这一定是因为它只是尝试序列化对象结果,但我找不到任何关于它在 .NET 5 中应该如何工作的文档。
我的主要功能目前看起来像这样:
public static async Task Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(x =>
x.UseDefaultWorkerMiddleware())
.ConfigureAppConfiguration((_, builder) => builder
.AddJsonFile("local.settings.json", true)
.Build())
.ConfigureServices(ConfigureServices)
.Build();
await host.RunAsync();
}
我的项目位于这里,以防有人感兴趣:https : //github.com/sponsorkit/sponsorkit.io
目前,我的 .NET 5 工作在一个名为feature/signup-flow.
回答
在 .NET 5 中将 IActionResult 与 Azure Functions 一起使用?
您不能IActionResult在 .NET 5 中使用 Azure Functions返回。或者更一般地说,不能IActionResult使用隔离进程模型返回Azure Functions。来自文档的引用:
对于 HTTP 触发器,您必须使用 HttpRequestData 和 HttpResponseData 来访问请求和响应数据。这是因为在进程外运行时您无权访问原始 HTTP 请求和响应对象。
取而代之的是IActionResult,您需要返回HttpResponseData。示例代码在这里。
THE END
二维码