0

I have a C# webapi project that is communicating as a signalr client with another application. Periodically it throws this exception which causes it to crash. I can reproduce the exception by running some load tests on it, but despite searching high and low I can't pinpoint where this exception is occurring and prevent it.

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter()
at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action)
at System.Web.Util.SynchronizationHelper.<>c__DisplayClass22_0.<QueueAsynchronous>b__0(Task _)
at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter()
at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action)
at System.Web.Util.SynchronizationHelper.<>c__DisplayClass22_0.<QueueAsynchronous>b__0(Task _)
at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()
at System.Threading.Tasks.Task.Execute()<---

My application is .net 4.7 and the signalr hub is running .net 4.7. I'm pretty sure the server is not involved. If I restart the client, it picks up where it left off and continues working.

There are a number of async methods in the webapi client, and I've confirmed that all the ones called internally are awaited and they all have exception handlers in them. The webapi endpoints are also marked async.

I've spent a few days trying to get to the bottom of this exception. Any tips would be gratefully accepted! Happy to elaborate with more info as needed.

4

1 回答 1

0

好像你已经ThrowUnobservedTaskExceptions设置为true. 推荐的设置(也是 .NET 4.7 的默认设置)是false.

从堆栈跟踪中,我猜想 ASP.NET (pre-Core) 依赖于被忽略的未观察到的任务异常。虽然这并不理想,但也不足为奇。

我建议删除启用ThrowUnobservedTaskExceptions.

于 2019-06-18T01:45:50.500 回答