Sporadic System.Net.Http.HttpRequestException: Error while copying content to a stream.
Hi,
I receive this sporadic error in subject while calling HttpClient.PostAsync(url, content). I want to make sure that this is not related to my code.
public async Task<HttpResponseMessage> UploadPhotoAsync(string productId, MediaGallery media)
{
SetAuthenticationHeader();
string url = $"{_settings.GetMagentoProductsUrl()}/{productId}/media";
RootMedia root = new RootMedia() { Media = media };
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string inData = JsonSerializer.Serialize<RootMedia>(root, options);
HttpContent content = new StringContent(inData, Encoding.UTF8, "application/json");
var result = await _client.PostAsync(url, content);
if (!result.IsSuccessStatusCode)
{
string cont = await result.Content.ReadAsStringAsync();
if (result.StatusCode == System.Net.HttpStatusCode.BadRequest
|| result.StatusCode == System.Net.HttpStatusCode.Unauthorized
|| result.StatusCode == System.Net.HttpStatusCode.InternalServerError)
{
var error = MagentoError.FromJson(cont);
_logger.LogError($"{productId}-{media.FileContent.Name} cannot be uploaded. status code {(int)result.StatusCode} error {error?.message}");
}
}
return result;
}
Not sure why this occurs sporadically. This long stack trace points to my client.PostAsync line
System.Net.Http.HttpRequestException: Error while copying content to a stream.
---> System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
--- 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.GetResult(Int16 token)
at System.Net.Security.SslStream.<WriteSingleChunk>g__CompleteWriteAsync|177_1[TIOAdapter](ValueTask writeTask, Byte[] bufferToReturn)
at System.Net.Security.SslStream.WriteAsyncChunked[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory1 buffer) at System.Net.Security.SslStream.WriteAsyncInternal[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory
1 buffer)
at System.Net.Http.HttpConnection.WriteAsync(ReadOnlyMemory`1 source, Boolean async)
at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask) --- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask)
at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
t System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, 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.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
Can anyone shed some light on this?
Thanks,
Holy