ASP.NET Web API: PushStreamContent flush does not flush

混江龙づ霸主 提交于 2019-12-05 05:41:56

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

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));
    }
  }
}

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.

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