HttpClient Connection Reset By Peer when using HttpClientExtensions
Tested using .NET Core Versions 2.1 and 2.2
We have a HttpClient that makes some requests using the PostAsJsonAsync and PutAsJsonAsync from the HttpClientExtensions in the System.Net.Http.Formatting assembly but in some cases we are getting an exception with a message saying the Connection was reset by peer. The Stack Trace we have is:
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. --- End of inner exception stack trace --- at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token) at System.Net.Http.HttpConnection.FillAsync() at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
Now the strange and unbelievable solution we happen to achieve is to create a dummy Handler that just prints the Request and Response content to the ILogger as follows:
public class LoggingHandler : DelegatingHandler
{
ILogger<TenantSeeder> logger;
public LoggingHandler(HttpMessageHandler innerHandler, ILogger<TenantSeeder> logger)
: base(innerHandler)
{
this.logger = logger;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
logger.LogTrace("Request:");
logger.LogTrace(request.ToString());
if (request.Content != null)
{
logger.LogTrace(await request.Content.ReadAsStringAsync());
}
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
logger.LogTrace("Response:");
logger.LogTrace(response.ToString());
if (response.Content != null)
{
logger.LogTrace(await response.Content.ReadAsStringAsync());
}
return response;
}
}
When using that Handler the request completes successfully.
Some more info: the connection is using SSL and when called directly from Postman it works as expected and the JSON being sent is very simple:
var response = await httpClient.PostAsJsonAsync("api/tenants", new
{
Name = tenant.CompanyName,
UniqueName = uniqueName,
ClientId = uniqueName,
ClientSecret = $"{uniqueName}secret",
Enabled = tenant.IsActivated
})
Are there any updates on this? I am having the same problem, getting random connection reset by peerer when there are many requests to an api hosted on IIS. Till now I could not find the root cause.
Same problem here when I run my code on a 4 core Linux azure vm, one a 2 cores vm things seems ok
Triage: It looks like server closed the connection. To make it actionable we need either a repro, or traces. Trying out 3.0 might be good first step. Closing for now, we can reopen when there is tracing (Wireshark) or repro available.
I face the same problem too, I think it is big problem of Net Core 3.0
Same problem. It occurred in my program using Net Core 3.1