在使用异步流返回时,并不是实时刷新逐条返回结果,而是一次返回结果 #1943
Replies: 11 comments 16 replies
-
Dear @alivy ! |
Beta Was this translation helpful? Give feedback.
-
When there is text/event-stream in the response body, the returned result is loaded and returned at once, not refreshed in real time. |
Beta Was this translation helpful? Give feedback.
-
@alivy @raman-m 我不懂中文,所以可能无法用中文表达清楚。I would like to learn your language though. I think, you should wait the latest changes that were pushed to the develop branch. We are not copying the request body and response body directly anymore. |
Beta Was this translation helpful? Give feedback.
-
Due to environmental factors, the code cannot be made public, but I can describe the scenario to you : |
Beta Was this translation helpful? Give feedback.
-
The scene is as follows: |
Beta Was this translation helpful? Give feedback.
-
When I tried to modify the addition of middleware in the pipeline to respond to data asynchronously, an error occurred; public class CustomResponseMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
public CustomResponseMiddleware(RequestDelegate next, IOcelotLoggerFactory loggerFactory)
: base(loggerFactory.CreateLogger<CustomResponseMiddleware>())
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
var response = httpContext.Items.DownstreamResponse();
const string streamingContent = "text/event-stream";
if (httpContext.Response.Headers["Content-Type"] == streamingContent
|| httpContext.Response.ContentType == streamingContent
|| response.Content.Headers.ContentType?.ToString() == streamingContent)
{
//await ForwardStream(httpContext.Response.Body, response);
var stream = await response.Content.ReadAsStreamAsync();
await WriteServerStreamEventsAsync(httpContext, stream);
}
await _next.Invoke(httpContext);
}
private static async Task WriteServerStreamEventsAsync(HttpContext httpContext, Stream contentStream)
{
var responseStream = httpContext.Response.Body;
await contentStream.CopyToAsync(responseStream, httpContext.RequestAborted);
await responseStream.FlushAsync(httpContext.RequestAborted);
}
} The error message is as follows: Microsoft.AspNetCore.Server.Kestrel[13] |
Beta Was this translation helpful? Give feedback.
-
@alivy could you tell us which Ocelot version you are using? |
Beta Was this translation helpful? Give feedback.
-
Ocelot 22.0.1 |
Beta Was this translation helpful? Give feedback.
-
@alivy @raman-m it seems we need to disable buffering on the response: |
Beta Was this translation helpful? Give feedback.
-
@alivy do you have a solution for this issue? |
Beta Was this translation helpful? Give feedback.
-
app.UseOcelot().Wait();
const string STREAMING_CONTENT = "text/event-stream";
app.Use(async (context, next) => {
DownstreamResponse response = context.Items.DownstreamResponse();
if (context.Response.Headers["Content-Type"] == STREAMING_CONTENT
|| context.Response.ContentType == STREAMING_CONTENT
|| response?.Content.Headers.ContentType?.ToString() == STREAMING_CONTENT) {
context.Features.Get<IHttpResponseBodyFeature>()?.DisableBuffering();
}
await next();
});
app.Run(); The code above works for me. |
Beta Was this translation helpful? Give feedback.
-
Steps to Reproduce the Problem
自定义拓展代码如下
Beta Was this translation helpful? Give feedback.
All reactions