2

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:

Application: 2014-05-20T03:24:57  PID[2088] Warning     SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---> 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.<.ctor>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.<KeepAlive>b__0(Object state)
Application:    at Microsoft.AspNet.SignalR.Infrastructure.TaskQueue.<Enqueue>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: ---> (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.<.ctor>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.<KeepAlive>b__0(Object state)
Application:    at Microsoft.AspNet.SignalR.Infrastructure.TaskQueue.<Enqueue>b__1(Func`2 next, Object nextState)
Application:    at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2](Func`3 func, T1 arg1, T2 arg2)<---

All of my code is wrapped in try/catch statements and I have an unobserved exception handler defined using TaskScheduler.UnobservedTaskException. If I could catch it, I could flatten the aggregate exception and process each individually.

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.

Ender2050
  • 6,912
  • 12
  • 51
  • 55
  • Could you try: `Application.ThreadException += new ThreadExceptionEventHandler (ErrorHandlerForm.Form1_UIThreadException); // Set the unhandled exception mode to force all Windows Forms errors to go through // our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // Add the event handler for handling non-UI thread exceptions to the event. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);` – Jevgeni Geurtsen May 20 '14 at 14:05
  • `TaskScheduler.UnobservedTaskException` does not catch the exception at all? – Yuval Itzchakov May 20 '14 at 14:22
  • TaskScheduler.UnobservedTaskException is not catching it... – Ender2050 May 20 '14 at 14:30
  • The Dutch Man -- Is it possible to use Application.ThreadException in an ASP.NET Web Api project?? – Ender2050 May 20 '14 at 14:37

1 Answers1

1

You should be able to catch it with HttpApplication.Error, more details here:

Processing Unhandled Exceptions

Yet I'm surprised that none of your catch block is getting triggered. Are you sure you're wrapping every Task? You may want to try something like task.WithObservation() from here.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • 1
    Thank you for this. I tried HttpApplication.Error and it's not catching anything, but I'm still working on your Task.WithObservation() suggestion. That link has led me to several of your other posts - all really good, learning a lot from them. – Ender2050 May 21 '14 at 13:46
  • I'm still trying to figure this out. Thanks again for the help so far - learned a lot from your posts. – Ender2050 May 29 '14 at 04:35
  • @Ender2050, you could try to catch it first as [first-chance exception](http://msdn.microsoft.com/en-us/library/dd997368(v=vs.110).aspx). This won't solve the problem, but may help with troubleshooting. – noseratio May 29 '14 at 04:39
  • 2
    This error is intermittent so I'm not certain - but I think I have this working using the approach you outlined in your second link http://stackoverflow.com/questions/23766808/why-doesnt-this-exception-get-thrown/23771876#23771876. Very helpful, thank you again. – Ender2050 Jun 09 '14 at 13:33