C# HttpClient - Unable to read data from the transport connection: Connection reset by peer

98 views Asked by At

I have an API that is mounted in the Azure portal, and in one method I used httpclient to obtain a catalog of another API that is in a virtual machine and in http (Not https).

If I try it locally, it always works fine, but when I use the production version within the Azure portal, it works as long as the data does not exceed 1024 bytes, but when the response returns more than 1024 bytes it sends this error:

(System.Net.Http.HttpRequestException: An error occurred while sending the request.
    ---> System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer.
    ---> System.Net.Sockets.SocketException (104): Connection reset by peer
    --- End of inner exception stack trace ---
    at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
    at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Int32>.GetResult(Int16 token)
    at System.Net.Http.HttpConnection.InitialFillAsync(Boolean async)
    at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
    --- End of inner exception stack trace ---
    at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
    at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
    at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
    at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
    at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
    at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
    at RestSharp.RestClient.ExecuteRequestAsync(RestRequest request, CancellationToken cancellationToken))

Is there a missing configuration within the Azure portal? or within the API code? Or is it some configuration of the API that returns the response and is in http?

1

There are 1 answers

4
Paul-Jan On

I am going to go out on a limb and guess the Azure/1024 byte limit might not be the cause of your issue. Are you using the proper pattern of re-using the HttpClient instance (e.g. make it a static field or get it from a singleton wrapper), avoiding creating a new one for every single request??

If you just use new HttpClient() whenever you need one, this is exactly the sort of behavior you get under load as the underlying connection pool runs out of ports to use.

There is an article with some background information available here, but that might make it sound more complicated then it is. The gist is to just use something like

private static HttpClient Client = new HttpClient();