ASP.NET Web API: PushStreamContent flush does not flush

心不动则不痛 提交于 2019-12-22 05:01:25

问题


I'm using PushStreamContent in ASP.NET Web API to push events from server to client (using Server-Sent Events). After each sent event, I call Flush on the Stream to push the buffered data to the client. However, I noticed that the flushing does not (always) happen. Sometimes, part of the data is sent to the client, and the rest is sent when the next event is written (which could happen seconds later).

Here's a code sample:

public class MyController : ApiController
{
  private static readonly string[] LineSeparators 
    = new[] { Environment.NewLine };

  public HttpResponseMessage GetData(string id)
  {
    var response = Request.CreateResponse();
    response.Content = new PushStreamContent(
      new Func<Stream, HttpContent, TransportContext, Task>(StartStream),
      new MediaTypeHeaderValue("text/event-stream") { CharSet = "UTF-8" });
    return response;
  }

  private async Task StartStream(Stream outputStream, HttpContent content, TransportContext context)
  {
    using (outputStream)
    using (var writer = new StreamWriter(outputStream, new UTF8Encoding(false)))
    {
      writer.NewLine = "\n";
      while (true)
      {
        WriteEvent(writer, "ping", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture));
        await Task.Delay(TimeSpan.FromSeconds(1));
      }
    }
  }

  private static void WriteEvent(TextWriter writer, string eventType, string data)
  {
    writer.WriteLine("event:" + eventType);
    writer.WriteLine("data:" + data);
    writer.WriteLine();
    writer.Flush(); // StreamWriter.Flush calls Flush on underlying Stream
  }
}

How can I disable the buffering of the data or force the flushing of the data?


回答1:


I got it working.

In my case buffering was an isssue. I had to

1) disable gzip for my responses <urlCompression doStaticCompression="true" doDynamicCompression="false" />

2) Make sure that the proxy on Prod (Nginx) wasn't buffering either




回答2:


The source of the issue is the Stream being flushed.

At your code sample you warp the original stream with StreamWriter and then flush the StreamWriter .

You need to flush the original stream as well:

private async Task StartStream(Stream outputStream, HttpContent content, TransportContext context)
{
  using (outputStream)
  using (var writer = new StreamWriter(outputStream, new UTF8Encoding(false)))
  {
    writer.NewLine = "\n";
    while (true)
    {
      WriteEvent(writer, "ping", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture));
outputStream.Flush();
      await Task.Delay(TimeSpan.FromSeconds(1));
    }
  }
}



回答3:


After spending an entire day trying to figure out where the problem is, and going as far as to (desperately) giving out a bounty, I found out that the problem lied in the fact that I was using HttpSelfHostServer, and needed to configure TransferMode = TransferMode.Streamed on the HttpSelfHostConfiguration. That's all.



来源:https://stackoverflow.com/questions/29793308/asp-net-web-api-pushstreamcontent-flush-does-not-flush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!