question

Holysmokes-6260 avatar image
0 Votes"
Holysmokes-6260 asked AgaveJoe commented

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, ReadOnlyMemory`1 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







dotnet-aspnet-core-generaldotnet-runtime
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The error is coming from the remote host closing the socket. Usually we see this error when the remote host is not running. Possible causes the host reached idle timeout and is taking too long to start up. The host might be restarting unexpectedly due to a problem with the code or design.

0 Votes 0 ·

0 Answers