Reputation: 1162
We are load testing a simple ASP.NET MVC web API application. When we send a couple of hundred requests a second over 6 threads (our timeout is set to 5 seconds), we get either one of the following exceptions:
"System.Threading.Tasks.TaskCanceledException A task was canceled"
or
"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}".
While executing the requests we notice that at some stage the pace of execution slows down and we get an exception. We are guessing that the first error is related to timeout. We suspect that the other error may be due to connection limitations with the web API application.
We have a WCF application that we send as many requests to as well and that causes no problems.
Both applications are hosted on IIS 7 on the same machine.
Is there a limit on the number of connections in MVC web API? Is there a difference between that and the way WCF handles connections?
EDIT:
The client code:
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);
}
}
We found this link: Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?
We think that may be the issue...
Upvotes: 2
Views: 4796
Reputation: 22323
This is possibly not a limitation in WebAPI; rather, an issue with how the TCP stack handles connection pooling. Effectively, the TCP stack has a limit on the number of "Ephermeral" ports that can be created, approximately 5,000. You can modify the registry to adjust the MaxUserPort
value. See http://support.microsoft.com/kb/196271
1.Start Registry Editor.
2.Locate the following subkey in the registry, and then click Parameters: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
3.On the Edit menu, click New, and then add the following registry entry: Value Name: MaxUserPort
Value Type: DWORD Value data: 65534 Valid Range: 5000-65534 (decimal) Default: 0x1388 (5000 decimal)
That being said, this may be a code segment that bears some review, as there probably is a better way to accomplish your task than to spawn multiple connections to the server simultaneously from a single client.
Upvotes: 3