如何捕获此SignalR异常

时间:2014-05-20 13:56:37

标签: c# asp.net signalr async-await

我有一个ASP.NET Web Api,它使用SignalR将实时数据传递给移动客户端。一切都运行良好,但我在跟踪日志中看到一个异常,我似乎无法捕获或处理此异常:

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)<---

我的所有代码都包含在try / catch语句中,并且我使用TaskScheduler.UnobservedTaskException定义了一个未观察到的异常处理程序。如果我能抓住它,我可以压扁聚合异常并单独处理每个异常。

有谁知道如何捕获或处理此异常?它似乎每隔几分钟发生一次。提前谢谢 - 我一直试图解决这个问题。

1 个答案:

答案 0 :(得分:1)

您应该能够通过HttpApplication.Error抓住它,更多详情请点击此处:

Processing Unhandled Exceptions

然而,我很惊讶你的catch块没有被触发。您确定要包装每个 Task吗?您可能希望尝试使用here中的task.WithObservation()