[英]Using IActionResult with Azure Functions in .NET 5?
After migrating my Azure Functions project to .NET 5, it has started wrapping my responses in a weird wrapper class.将我的 Azure Functions 项目迁移到 .NET 5 后,它开始将我的响应包装在一个奇怪的包装器 class 中。
For instance, consider the following endpoint:例如,考虑以下端点:
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"));
}
Before, it would return:之前,它会返回:
{
"someValue": "hello world"
}
But now, it returns:但现在,它返回:
{
"Value": {
"SomeValue": "hello world"
},
"Formatters": [],
"ContentTypes": [],
"DeclaredType": null,
"StatusCode": 200
}
I get that it must be because it just tries to serialize the object result, but I can't find any documentation on how this is supposed to work in .NET 5.我知道这一定是因为它只是试图序列化 object 结果,但我找不到任何关于它应该如何在 .NET 5 中工作的文档。
My main function currently looks like this:我的主要 function 目前看起来像这样:
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();
}
My project is located here, in case someone are interested: https://github.com/sponsorkit/sponsorkit.io我的项目位于此处,以防有人感兴趣: https://github.com/sponsorkit/sponsorkit.io
Currently, my .NET 5 work is on a branch called feature/signup-flow
.目前,我的 .NET 5 工作在一个名为feature/signup-flow
的分支上。
Using IActionResult with Azure Functions in .NET 5?在 .NET 5 中将 IActionResult 与 Azure 函数一起使用?
You can't return IActionResult
with Azure Functions in .NET 5 .您不能使用 .NET 5 中的 Azure 函数返回IActionResult
。 Or more generally, you can't return IActionResult
with Azure Functions using the isolated process model.或者更一般地说,您不能使用 Azure 函数返回IActionResult
使用隔离进程 model。 Quote from the docs:从文档中引用:
For HTTP triggers, you must use HttpRequestData and HttpResponseData to access the request and response data.对于 HTTP 触发器,您必须使用 HttpRequestData 和 HttpResponseData 来访问请求和响应数据。 This is because you don't have access to the original HTTP request and response objects when running out-of-process.这是因为在进程外运行时,您无权访问原始 HTTP 请求和响应对象。
Instead of IActionResult
, you need to return HttpResponseData
.您需要返回HttpResponseData
而不是IActionResult
。 Example code is here .示例代码在这里。
To return an object from .NET 5 Azure Functions, use the following code:要从 .NET 5 Azure 函数返回 object 函数,请使用以下代码:
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(obj);
return response;
Was pleasantly surprised to find that returning Task from a "dotnet-isolated" function workins in Core 6. Saved me a few hours as I do not need to change to Task in a number of functions惊喜地发现从“dotnet-isolated” function 返回 Task 在 Core 6 中工作。节省了我几个小时,因为我不需要在许多功能中更改为 Task
After searching around and wanting to stay in the context of the IActionResult object, I ended up leveraging the ObjectResult object so that I can standardize all the service responses with the standardize JSON messages.在搜索并希望留在 IActionResult object 的上下文中之后,我最终利用了 ObjectResult object,以便我可以使用标准化的 Z0ECD11C1D7A287401D148A23BBDA7F8 消息标准化所有服务响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.