Azure Functions:无法设置 StatusCode,因为响应已经开始
Azure Functions: StatusCode cannot be set because the response has already started
我正在尝试从我的 Azure 函数流式传输数据,一切正常,但在执行后出现错误。
这是代码:
[FunctionName("QueryData")]
public async Task QuerySingleMessages([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
var message = await req.GetPostMessage<Message>();
var response = req.HttpContext.Response;
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
var sw = new StreamWriter(response.Body);
foreach (var msg in this.messageReaderService.FindLatestMessages(message.keys))
{
var json = System.Text.Json.JsonSerializer.Serialize(
msg,
new JsonSerializerOptions {PropertyNameCaseInsensitive = false});
await sw.WriteAsync(json);
}
await sw.FlushAsync();
await sw.DisposeAsync();
}
这是错误:
[2022-05-06T15:15:17.034Z] Executed 'QueryData' (Succeeded, Id=df70d5a7-37d7-40ef-b446-ecceb943e454, Duration=1350ms)
[2022-05-06T15:15:17.047Z] An unhandled host error has occurred.
[2022-05-06T15:15:17.048Z] Microsoft.AspNetCore.Server.Kestrel.Core: StatusCode cannot be set because the response has already started.
是否有我可以更改的配置(或其他配置)以阻止 host error
发生?
谢谢
从我们这边重现后,当我们尝试修复 return 类型和 return EmptyResult 即 return new EmptyResult();
.
时,我们能够完成这项工作
[FunctionName("Function1")]
public static async Task<EmptyResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
var message = await req.GetPostMessage<Message>();
var response = req.HttpContext.Response;
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
var sw = new StreamWriter(response.Body);
foreach (var msg in this.messageReaderService.FindLatestMessages(message.keys))
{
var json = System.Text.Json.JsonSerializer.Serialize(
msg,
new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
await sw.WriteAsync(json);
}
await sw.FlushAsync();
await sw.DisposeAsync();
return new EmptyResult();
}
我正在尝试从我的 Azure 函数流式传输数据,一切正常,但在执行后出现错误。
这是代码:
[FunctionName("QueryData")]
public async Task QuerySingleMessages([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
var message = await req.GetPostMessage<Message>();
var response = req.HttpContext.Response;
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
var sw = new StreamWriter(response.Body);
foreach (var msg in this.messageReaderService.FindLatestMessages(message.keys))
{
var json = System.Text.Json.JsonSerializer.Serialize(
msg,
new JsonSerializerOptions {PropertyNameCaseInsensitive = false});
await sw.WriteAsync(json);
}
await sw.FlushAsync();
await sw.DisposeAsync();
}
这是错误:
[2022-05-06T15:15:17.034Z] Executed 'QueryData' (Succeeded, Id=df70d5a7-37d7-40ef-b446-ecceb943e454, Duration=1350ms)
[2022-05-06T15:15:17.047Z] An unhandled host error has occurred.
[2022-05-06T15:15:17.048Z] Microsoft.AspNetCore.Server.Kestrel.Core: StatusCode cannot be set because the response has already started.
是否有我可以更改的配置(或其他配置)以阻止 host error
发生?
谢谢
从我们这边重现后,当我们尝试修复 return 类型和 return EmptyResult 即 return new EmptyResult();
.
[FunctionName("Function1")]
public static async Task<EmptyResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
var message = await req.GetPostMessage<Message>();
var response = req.HttpContext.Response;
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
var sw = new StreamWriter(response.Body);
foreach (var msg in this.messageReaderService.FindLatestMessages(message.keys))
{
var json = System.Text.Json.JsonSerializer.Serialize(
msg,
new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
await sw.WriteAsync(json);
}
await sw.FlushAsync();
await sw.DisposeAsync();
return new EmptyResult();
}