9

I'm getting the following error in logs for WebAPI

System.Web.HttpException (0x800703E3): The client disconnected. at System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult asyncResult) at System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory 1.FromAsyncTrimPromise 1.Complete(TInstance thisRef, Func 3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.IO.StreamReader.d__97.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.IO.StreamReader.d__62.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.OwinRequest.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.d__22.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware 1.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Web.API.Middleware.OwinMiddleware.d__1.MoveNext() in D:\UAT\Web.API\Middleware\OwinMiddleware.cs:line 49

How can I handle and ignore these exceptions by exception filter? Why this error occurs and how can I reproduce that? I wanna catch and ignore only The client disconnected but not all HttpException

I saw this similar question but how can I do that in exception filter?

Pr.Dumbledor
  • 636
  • 1
  • 11
  • 29

2 Answers2

9

You can ignore these exceptions.

The exception says what the problem is: "The client disconnected". It means that the client initiated a request but then disconnected before it bothered to read all of the response.

There are any numbers of reasons why that might be the case, but (unless you have a dodgy internet connection at your server end) it is almost certainly an issue at the client end. I regularly see these myself and mostly it seems to be related to a bot. So I filter them out and ignore them.

Brian Cryer
  • 2,126
  • 18
  • 18
  • 2
    I don't use the ApiAPI, but I get the same error from my IIS server with my .NET application which is why I recognise it and know it can be ignored. In terms of "how do you do that"? Find where the error is being logged (in my case I'm catching it in `Application_Error`) and the test I'm using is basically just `if (ex.Message.StartsWith("The client disconnected")) { // Ignore }` – Brian Cryer Jul 05 '17 at 11:27
  • I got this error as well when sending request to my API but the error keep prompting even I'm in good connection whereas other requests that are firing after the previous request that got this error are success. – Kenny Eng Sep 24 '21 at 08:05
-1
try
{
   // Open Connection
   // Do stuff
}
catch (HttpException  ex)
{
   // Log it?
   // Ignore it? 
   // Do what you want with it?
}
finally
{
   // Close connection
}
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
  • 1
    I wanna catch and ignore only `The client disconnected` but not all HttpException – Pr.Dumbledor Jul 05 '17 at 11:02
  • Well you can't catch that only, because that is a HttpException... so you could catch all HttpException, and inside the catch check if its message contains `The client disconnected` and handle that, then if it's not that particular subset of `HttpException` just throw the exception... But honestly that sounds like a bad idea, why would you want to ignore `The client disconnected`? – Thomas Cook Jul 05 '17 at 11:04
  • Because I got many log messages, I don't need log message with client disconnected – Pr.Dumbledor Jul 05 '17 at 11:05
  • Ok, so just check if the `HttpException` contains client disconnected message. If it does, don't log it... – Thomas Cook Jul 05 '17 at 11:06
  • 2
    Or, if you want an extensible logging ruleset, you could create a dictionary of Exception messages mapped to a logging type enum. Then query that dictionary with the exception message to get the logging behaviour. This way if you change your mind later you only have to change the rules in one place (the dictionary). – Thomas Cook Jul 05 '17 at 11:07