title: How to catch this SignalR exception
body: <p>I have an ASP.NET Web Api that uses SignalR to communicate real-time data to mobile clients. Everything is working well, but I see an exception in my trace logs and I can't seem to catch or handle this exception:</p>
  
  <pre><code>Application: 2014-05-20T03:24:57  PID[2088] Warning     SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---&gt; System.Web.HttpException: The remote host closed the connection. The error code is 0x800704CD.
  Application:    at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
  Application:    at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
  Application:    at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async)
  Application:    at System.Web.HttpResponse.Flush()
  Application:    at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
  Application:    at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  Application:    at Microsoft.Owin.Host.SystemWeb.CallStreams.OutputStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  Application:    at Microsoft.AspNet.SignalR.Owin.ServerResponse.Write(ArraySegment`1 data)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BinaryTextWriter.&lt;.ctor&gt;b__4(ArraySegment`1 data, Object state)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.ChunkedWriter.Flush(Byte[] byteBuffer, Boolean flushEncoder)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.ChunkedWriter.Flush(Boolean flushEncoder)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.Flush()
  Application:    at Microsoft.AspNet.SignalR.Transports.ServerSentEventsTransport.PerformKeepAlive(Object state)
  Application:    at Microsoft.AspNet.SignalR.Transports.ServerSentEventsTransport.&lt;KeepAlive&gt;b__0(Object state)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.TaskQueue.&lt;Enqueue&gt;b__1(Func`2 next, Object nextState)
  Application:    at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2](Func`3 func, T1 arg1, T2 arg2)
  Application:    --- End of inner exception stack trace ---
  Application: ---&gt; (Inner Exception #0) System.Web.HttpException (0x800704CD): The remote host closed the connection. The error code is 0x800704CD.
  Application:    at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
  Application:    at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
  Application:    at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async)
  Application:    at System.Web.HttpResponse.Flush()
  Application:    at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
  Application:    at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  Application:    at Microsoft.Owin.Host.SystemWeb.CallStreams.OutputStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  Application:    at Microsoft.AspNet.SignalR.Owin.ServerResponse.Write(ArraySegment`1 data)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BinaryTextWriter.&lt;.ctor&gt;b__4(ArraySegment`1 data, Object state)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.ChunkedWriter.Flush(Byte[] byteBuffer, Boolean flushEncoder)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.ChunkedWriter.Flush(Boolean flushEncoder)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.BufferTextWriter.Flush()
  Application:    at Microsoft.AspNet.SignalR.Transports.ServerSentEventsTransport.PerformKeepAlive(Object state)
  Application:    at Microsoft.AspNet.SignalR.Transports.ServerSentEventsTransport.&lt;KeepAlive&gt;b__0(Object state)
  Application:    at Microsoft.AspNet.SignalR.Infrastructure.TaskQueue.&lt;Enqueue&gt;b__1(Func`2 next, Object nextState)
  Application:    at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2](Func`3 func, T1 arg1, T2 arg2)&lt;---
  </code></pre>
  
  <p>All of my code is wrapped in try/catch statements and I have an unobserved exception handler defined using <code>TaskScheduler.UnobservedTaskException</code>. If I could catch it, I could flatten the aggregate exception and process each individually. </p>
  
  <p>Does anyone know how I can catch or handle this exception? It seems to happen every few minutes. Thanks in advance - I've been trying to figure this out for a while.</p>