title: Owin SelfHost WebApi - client closing the connection during response raises an exception?
body: <p>I'm running an Owin Selfhost based WebApi where i've put in an API unhandled exception logger via </p>
  
  <pre><code>config.Services.Add(typeof(IExceptionLogger), _apiExceptionLogger);
  </code></pre>
  
  <p>Relevant part of ApiExceptionLogger:</p>
  
  <pre><code>    public override void Log(ExceptionLoggerContext context)
      {
          if (context == null || context.ExceptionContext == null) return;
  
          Logger.Error("Unhandled exception from Web API", context.ExceptionContext.Exception);
      }
  </code></pre>
  
  <p>The cases it's catching and logging regularly are ones where the client requests a dataset and then closes the connection while the results (JSON) are being sent back - people making a request in chrome, and then hitting the X button before all results come back :P</p>
  
  <p>I've pasted a stacktrace below for completeness, just want to know two things:</p>
  
  <ul>
  <li>Is this regular/expected behavior? AFAIK it is...I'm running a pretty default API and pipeline</li>
  <li>Is there any way to handle this? Essentially stop request processing more gracefully in case of a cancellation (the cancellation tokens peppered throughout the request pipeline do come to mind, but doesn't look like they do much in this case, after all the tokens only support co-operative cancellation)</li>
  </ul>
  
  <p>I haven't done any deep dive on the sequence of events happening at the socket level, so far this is only a logging nuisance.</p>
  
  <pre><code>System.Net.Http.HttpRequestException: Error while copying content to a stream. ---&gt; System.IO.IOException ---&gt; System.Net.HttpListenerException: The I/O operation has been aborted because of either a thread exit or an application request
     at System.Net.HttpResponseStream.EndWrite(IAsyncResult asyncResult)
     at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
     --- End of inner exception stack trace ---
     at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
     at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
     --- End of inner exception stack trace ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Web.Http.Owin.HttpMessageHandlerAdapter.&lt;SendResponseContentAsync&gt;d__20.MoveNext()    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Web.Http.Owin.HttpMessageHandlerAdapter.&lt;SendResponseContentAsync&gt;d__20.MoveNext()
  </code></pre>