2

I have this code called from an ASP.NET MVC controller:

protected string PostData(string url, ByteArrayContent content)
        {
            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromDays(1);
                return client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            }
        }

If I am posting data to a REST service which takes some time to execute, I get this error:

System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> 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: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
   at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Libcor.Services.Ssp.External.ExternalServiceBase.PostData(String absoluteUrl, ByteArrayContent content)

The REST service still completes it's work in the background but the ASP.NET MVC gets this exception. Works fine for shorter running services. How do I prevent this from happening?

c0D3l0g1c
  • 3,020
  • 5
  • 33
  • 71

2 Answers2

6

Adding this helped:

_client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
_client.DefaultRequestHeaders.Add("Keep-Alive", "3600");

I managed to keep the request alive long enough for it to complete gracefully and receive a valid response.

c0D3l0g1c
  • 3,020
  • 5
  • 33
  • 71
  • 1
    This resolved a similar SSL issue I was having that was reporting the same error. Good solution, was a very weird error for me to track down since a library was working for one app but not another even though the request was the exact same. Either way, thanks :) – zgc7009 Aug 10 '21 at 15:56
3

The solution for me was updating the https protocol from the default ssl3 to tls via https://stackoverflow.com/a/46223433/352432

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Steve
  • 25,806
  • 2
  • 33
  • 43