【发布时间】:2014-10-22 15:04:27
【问题描述】:
我们正在对一个简单的 ASP.NET MVC Web API 应用程序进行负载测试。当我们在 6 个线程上每秒发送几百个请求时(我们的超时设置为 5 秒),我们会遇到以下异常之一:
"System.Threading.Tasks.TaskCanceledException A task was canceled"
或
"InnerException {"An error occurred while sending the request."}
System.Exception {System.Net.Http.HttpRequestException}
InnerException {"Unable to connect to the remote server"}
System.Exception {System.Net.WebException}
InnerException {"An operation on a socket could not be performed
because the system lacked sufficient buffer space or because a queue was full"}
System.Exception {System.Net.Sockets.SocketException}".
在执行请求时,我们注意到在某个阶段执行速度变慢了,并且出现了异常。我们猜测第一个错误与超时有关。我们怀疑其他错误可能是由于 Web API 应用程序的连接限制。
我们有一个 WCF 应用程序,我们也向它发送尽可能多的请求,这不会导致任何问题。
两个应用程序都托管在同一台机器上的 IIS 7 上。
MVC Web API 中的连接数有限制吗?这与 WCF 处理连接的方式有区别吗?
编辑:
客户端代码:
for (int i = 0; i < 20000; i++)
{
try
{
const string _mediaTypeHeaderValue = "application/json";
var _serialisedRequest = "json request";
var _content = new StringContent(_serialisedRequest);
_content.Headers.ContentType = new MediaTypeHeaderValue(_mediaTypeHeaderValue);
using (var _httpClient = new System.Net.Http.HttpClient())
{
_httpClient.Timeout = new TimeSpan(0, 0, 5);
Console.WriteLine(i);
var _responseMessage = _httpClient.PostAsync("url", _content).Result;
if (_responseMessage.StatusCode != HttpStatusCode.OK)
{
var stringResult = _responseMessage.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception genericException)
{
Console.WriteLine(genericException.InnerException);
}
}
我们找到了这个链接: Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?
我们认为这可能是问题所在......
【问题讨论】:
-
这个异常是发生在客户端还是服务器端?
-
客户端,请求甚至没有到达服务器。
-
那看起来好像你在测试客户端中泄漏了连接。请把代码贴出来好吗?
标签: asp.net asp.net-mvc wcf