ConnectionResetException: An existing connection was forcibly closed by the remote host on Rest API call for big file

Viewed 29

I have an C# WebAPI that let user upload file in chunks. The application works fine for http. But when I added https for my API I found out it works fine for small size of files, but for bigger file sizes (>60MB) the request just waits for 2 minutes and then just fails. After looking at the server log I found this exception:

Microsoft.AspNetCore.Connections.ConnectionResetException: 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.
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.<GetResult>g__ThrowSocketException|5_0(SocketError e)
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.GetResult(Int16 token)
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive()
   --- End of inner exception stack trace ---
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.ReadAsyncInternal(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
   at System.IO.Pipelines.PipeReader.CopyToAsyncCore[TStream](TStream destination, Func`4 writeAsync, CancellationToken cancellationToken)
   at Infrastructure.LocalStorage.Domain.UploadFile.UploadChunkFileStreamService.SaveFileChunkAsync(UploadFileCommand command, String fileChunkPath)

What's even more crazy is it works fine for one server (we set up https on dev environment) but not for the other server. In both cases we are using IIS. Any idea how we can fix this issue? Am I missing any IIS configuration for HTTPS?

Note:

  • we already made uploadReadAheadSize bigger to avoid 413 error.
  • added the following code to my server as suggested on this post (however that's a SocketException NOT ConnectionResetException although the message are same)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 
  • the file is being uploaded by a react app from the front-end.
1 Answers

If you are posting a lot of data, you need to specify the request limit, length and execution maximum time in Web.config. The answer comes from this thread.

<requestLimits maxAllowedContentLength="2147483647" />
<httpRuntime targetFramework="4.6.1" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />

In addition, this explains the setting in detail.

Related