The remote host closed the connection Error, how fix? The remote host closed the connection Error, how fix? asp.net asp.net

The remote host closed the connection Error, how fix?


I see this a lot in the logs of a website I built.

AFAIK that exception means that the client broke the connection (went to another page / closed the browser) before the page had finished loading. If the client is downloading a file this error is even more likely as they have more time to decide to quit / move on.

The error is not visible to the users* - so I have just removed it from the Elmah logs.

*The site allows only authenticated users. I can see from Elmah who experienced the error and when. I've asked the users and not one person has ever reported experiencing this bug in the client.


I came across that error when user cancelled file download. On the server side it produces an Exception with message The remote host closed the connection. The error code is 0x800703E3. However, the exception does not have any distinct type that can be handled separately.

In odrer to handle it in ASP.NET MVC properly I added an exception logger:

public static class RegisterFilters{    public static void Execute(HttpConfiguration configuration)    {        configuration.Services.Add(typeof(IExceptionLogger), new WebExceptionLogger());    }}

And WebExceptionLogger class implementation:

public class WebExceptionLogger : ExceptionLogger{    private const int RequestCancelledByUserExceptionCode = -2147023901;    public override void Log(ExceptionLoggerContext context)    {        var dependencyScope = context.Request.GetDependencyScope();        var loggerFactory = dependencyScope.GetService(typeof(ILoggerFactory)) as ILoggerFactory;        if (loggerFactory == null)        {            throw new IoCResolutionException<ILoggerFactory>();        }        var logger = loggerFactory.GetTechnicalLogger<WebExceptionLogger>();        if (context.Exception.HResult == RequestCancelledByUserExceptionCode)        {            logger.Info($"Request to url {context.Request.RequestUri} was cancelled by user.");        }        else        {            logger.Error("An unhandled exception has occured", context.Exception);        }    }}

I noticed that this specifict error type has HResult = -2147023901, so this is what I'm filtering by.

Hope this helps.


I ran into this today, and for me, it was happening because the socket timed out waiting for the remote side to send.

In this example, an extremely (or deliberately) slow client might cause the exception.

I chose to resolve it by increasing the receive timeout on my sockets, since I wanted to support a few specific slower clients that were taxed with the high SSL requirements.