admin管理员组文章数量:1315033
I'm currently building an ASPNet Web Api that needs to stream a response as a string.
public async Task Stream(
[FromRoute] long id,
[FromBody] AddConversationMessageRequest request,
CancellationToken token)
{
request.ConversationId = id;
var httpContext = ControllerContext.HttpContext;
httpContext.Response.Headers.Append("Content-Type", "text/plain; charset=utf-8");
httpContext.Response.Headers.Append("X-Content-Type-Options", "nosniff");
httpContext.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
httpContext.Response.Headers.Append("Pragma", "no-cache");
httpContext.Response.Headers.Append("Expires", "0");
// Disable response buffering
httpContext.Response.StatusCode = StatusCodes.Status200OK;
await httpContext.Response.Body.FlushAsync(token);
await foreach (var response in Mediator.CreateStream(request, token))
{
await httpContext.Response.WriteAsync(response.Chunk", token);
await httpContext.Response.Body.FlushAsync(token);
}
}
The code works well except that when calling the API endpoint the result is buffered and only flushed when the string contains a \n\n character.
If I change this line
await httpContext.Response.WriteAsync(response.Chunk", token);
To
await httpContext.Response.WriteAsync(${response.Chunk\n\n}", token);
The response gets streamed correctly but of course with trailing \n\n.
I'm not able to find a way to completely disable the buffering or to flush the buffer after each whitespace for example.
本文标签: aspnet coreKestrel amp Aspnet Response BufferingStack Overflow
版权声明:本文标题:asp.net core - Kestrel & Aspnet Response Buffering - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741972165a2407904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论