【发布时间】:2014-07-08 20:04:08
【问题描述】:
我有一个 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 定义的未观察到的异常处理程序。如果我能抓住它,我可以展平聚合异常并单独处理每个异常。
有谁知道我如何捕捉或处理这个异常?它似乎每隔几分钟就会发生一次。在此先感谢 - 我一直在努力解决这个问题。
【问题讨论】:
-
你可以试试:
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); -
TaskScheduler.UnobservedTaskException根本没有捕捉到异常? -
TaskScheduler.UnobservedTaskException 没有捕捉到它......
-
荷兰人 -- 是否可以在 ASP.NET Web Api 项目中使用 Application.ThreadException??
标签: c# asp.net signalr async-await