Search code examples
.net.net-coredotnet-httpclient.net-standard-2.0

After over 1,000 successful calls, getting "An existing connection was forcibly closed"


I am doing some load testing of client and server code. As it gets going, the first 1,000 calls are just fine. But (on some runs), I start getting the following exception:

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.  
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..  
---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.

Some searching around shows that a common solution is to add the following code:

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

But this seems to be a fix for when none of the calls work. In my situation, I can get 1,000+ successful calls through before I start getting the above exception.

My real purpose in asking is that after this starts happening, I get a lot of failures of the same kind.

I am wondering if my HttpClient is in a faulted state after this happens? If it is, how do I fix that? (My HttpClient is static and if it fails, then all my calls will fail.)

Note: My code runs as a NuGet that needs to run in .Net Framework 4.7 and .Net Core 3.1.


Solution

  • I believe this issue was due to WSO2 API Manager limiting the number of connections. We change some settings on the WSO2 servers and this issue went away. (I don't know the changes as the WSO2 admin made them.)