HttpRequestException - An existing connection was forcibly closed by the remote host
See original GitHub issueWe have a web app calling a web service via API calls. We get the below error after creating a new record in that service.
Other code in our application seems to work ok, but for some reason, this particular issue is cropping up for this task only.
System.Net.Http.HttpRequestException HResult=0x80131620 Message=Error while copying content to a stream. Source=System.Net.Http StackTrace: at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__52.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__62.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at AcidWebApp.Repositories.LitmusRepository.<CreateLitmus>d__6.MoveNext() in C:\Users\Mark\projects\AcidWebApp\Repositories\LitmusRepository.cs:line 83
Inner Exception 1: IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Inner Exception 2: SocketException: An existing connection was forcibly closed by the remote host
We are using a helper class for this:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
namespace AcidWebApp.Helpers
{
public interface IHttpClientHelper
{
Task<HttpClient> GetHttpClient(HttpContext httpContext);
Task<HttpClient> GetHttpClient();
}
public class HttpClientHelper : IHttpClientHelper
{
private readonly string _apiUrl;
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpClientHelper(string apiUrl, IHttpContextAccessor httpContextAccessor)
{
_apiUrl = apiUrl;
_httpContextAccessor = httpContextAccessor;
}
public async Task<HttpClient> GetHttpClient(HttpContext httpContext)
{
var client = new HttpClient();
var accessToken = await httpContext.GetTokenAsync("access_token");
client.BaseAddress = new Uri(_apiUrl);
client.SetBearerToken(accessToken);
return client;
}
public async Task<HttpClient> GetHttpClient()
{
var client = new HttpClient();
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
client.BaseAddress = new Uri(_apiUrl);
client.SetBearerToken(accessToken);
return client;
}
}
}
This is the implementation that has the issue:
public async Task<Litmus> CreateLitmus(Litmus litmus)
{
var httpClient = await _httpClientHelper.GetHttpClient();
var response = await httpClient.PostAsJsonAsync("Litmus", litmus);
if (!response.IsSuccessStatusCode) return null;
var result = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<Litmus>(result);
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Found the issue, there is a
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected
this then closes the connection with an unhandled exception.I know how to resolve that, as it’s our poor data design that is the cause. closing this issue
var result = response.Content.ReadAsStringAsync().Result;
That will not play nice when under load.