dotnet / dotnet/core

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

Open
#4,407 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
22k
Forks
4.9k
Avg merge
5d 5h
Merged PRs (30d)
29

Description

httpClient.SendAsync throws this 'existing connection was forcibly closed by the remote host' error. This is not related to using block. Without using block also I'm getting same error. Also I tried setting servicepointmanager security prodtocol 'System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;'. This didn't help.

I'm using .net core 3.1. Exact Same code works perfect in .net framework. I believe this is happening only for cases with large response body from the target service.

```
try
{
using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false))
{
string content = await response.Content.ReadAsStringAsync();
response.EnsureSuccessStatusCode();
var responseEntity = JsonConvert.DeserializeObject(content);
return responseEntity;
}
}
catch (Exception ex)
{
string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: [{ex.GetType().Name}]{ex.Message}";
throw new CustomException(serviceAlias, HttpStatusCode.InternalServerError, errorMessage, ex);
}
```

```
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

An error occurred while sending the request

" at System.Net.Http.HttpConnection.d__61.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at System.Net.Http.HttpConnectionPool.d__40.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at System.Net.Http.HttpConnectionPool.d__39.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at System.Net.Http.RedirectHandler.d__4.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at System.Net.Http.DiagnosticsHandler.d__2.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at System.Net.Http.HttpClient.d__62.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n
at Microsoft.Ingestion.Stitch.Function.StitchServiceClient.d__11`1.MoveNext() in C:\\repo\\ICH.Ingestion.Offerjson.Stitch\\Src\\Stitch.Function\\Clients\\StitchServiceClient.cs:line 303"
```

```
" at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)\r\n
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)\r\n
at System.Net.FixedSizeReader.d__1.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Net.Security.SslState.ThrowIfExceptional()\r\n
at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)\r\n
at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)\r\n
at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()\r\n
at System.Net.Security.SslStreamInternal.d__34`1.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()\r\n
at System.Net.Http.HttpConnection.d__91.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Net.Http.HttpConnection.d__89.MoveNext()\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()\r\n
at System.Net.Http.HttpConnection.d__61.MoveNext()"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.