且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET Web API 2 - StreamContent 非常慢

更新时间:2023-02-15 20:23:35

我的 OWIN 自托管问题的解决方案是 StreamContent 缓冲区大小.StreamContent 的默认构造函数使用默认值 0x1000, 4Kb.在千兆网络上,以~60Kb/s 的速度传输 26Mb 文件大约需要 7 分钟才能完成.

The solution to my problem for OWIN self hosting was the StreamContent buffer size. The default constructor of StreamContent uses a default value of 0x1000, 4Kb. On a gigabit network, transfer of 26Mb file takes ~7 minutes to complete at rate of ~60Kb/s.

 const int BufferSize = 1024 * 1024;
 responseMessage = new HttpResponseMessage();
 responseMessage.Content = new StreamContent(fileStream, BufferSize);

将 bufferSize 修改为 1Mb 现在只需几秒钟即可完成下载.

Modifying the bufferSize to 1Mb now take only seconds to complete the download.

在 StreamContent SerializeToStreamAsync 中执行 StreamToStreamCopy,根据 此链接,性能会有所不同.一个合适的值大概是 80K.

In StreamContent SerializeToStreamAsync does a StreamToStreamCopy, according to this link the performance will differ. A suitable value is probably 80K.