的ASP.NET Web API 2 - StreamContent非常缓慢 [英] ASP.NET Web API 2 - StreamContent is extremely slow

查看:514
本文介绍了的ASP.NET Web API 2 - StreamContent非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经从移植到WCF的Web API(SelfHost)的项目,并在此过程中,我们提供了一个Web应用程序时,发现一个巨大的增长放缓。现在40-50秒比3秒previously。

We've ported a project from WCF to Web API (SelfHost) and during the process we noticed a huge slowdown when serving out a web application. Now 40-50 seconds vs 3 seconds previously.

我已经通过添加AspNet.WebApi和OwinSelfHost不同的NuGet pacakges具有以下控制器再现一个简单的控制台应用程序问题:

I've reproduce the issue in a simple console application by adding the various Nuget pacakges for AspNet.WebApi and OwinSelfHost with the following controller:

var stream = new MemoryStream();
using (var file = File.OpenRead(filename))
{
    file.CopyTo(stream);
}
stream.Position = 0;

var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);

/// THIS IS FAST
response.Content = new ByteArrayContent(stream.ToArray());
/// THIS IS SLOW
response.Content = new StreamContent(stream);

response.Content.Headers.ContentType = new MediaTypeHeaderValue(System.Web.MimeMapping.GetMimeMapping(filename));            
response.Content.Headers.ContentLength = stream.Length;

你可以从code,唯一的区别看到的是StreamContent(slooooow)VS ByteArrayContent的用法。

As you can see from the code the only difference is the usage of StreamContent (slooooow) vs ByteArrayContent.

应用程序托管在Win10的机器上,并从我的笔记本电脑访问。
提琴手表明,它需要14秒,从服务器向使用StreamContent我的笔记本电脑得到一个单一的1MB文件而ByteArrayContent小于1秒。

The application is hosted on a Win10 machine and accessed from my laptop. Fiddler shows that it takes 14 seconds to get a single 1MB file from the server to my laptop using StreamContent while ByteArrayContent is less than 1s.

还要注意的是完整的文件读入内存,表明唯一的区别是使用的内容类

Also note that the complete file is read into memory to show that the only difference is the Content class used.

但奇怪的是,似乎它的转移本身是缓慢的。服务器用头快速/立即响应,但数据需要很长时间如图所示的提琴手定时信息到达:

The strange thing is that it seems that its the transfer itself that is slow. The server responds with the headers quickly/immediately, but the data takes a long time to arrive as shown by the Fiddler timing info:

GotResponseHeaders: 07:50:52.800
ServerDoneResponse: 07:51:08.471

完整的时序信息:

Complete Timing Info:

== TIMING INFO ============
ClientConnected:    07:50:52.238
ClientBeginRequest: 07:50:52.238
GotRequestHeaders:  07:50:52.238
ClientDoneRequest:  07:50:52.238
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect:     15ms
HTTPS Handshake:    0ms
ServerConnected:    07:50:52.253
FiddlerBeginRequest:07:50:52.253
ServerGotRequest:   07:50:52.253
ServerBeginResponse:07:50:52.800
GotResponseHeaders: 07:50:52.800
ServerDoneResponse: 07:51:08.471
ClientBeginResponse:07:51:08.471
ClientDoneResponse: 07:51:08.471

Overall Elapsed:    0:00:16.233

有谁知道什么可以解释行为的差异引擎盖下回事?

Does anyone know what's going on under the hood that could explain the difference in behavior?

推荐答案

解决我的OWIN自托管的问题是StreamContent缓冲区大小。 StreamContent的默认构造函数使用的为0x1000,4KB缺省值。在一个千兆网络,26MB文件的传输时〜7个分钟的〜60KB / s的速率来完成。

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

修改缓冲区大小为1MB,现在只需要几秒钟即可完成下载。

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

这篇关于的ASP.NET Web API 2 - StreamContent非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆