NullReferenceException in System.Runtime.CompilerServices.As

本文关键字:CompilerServices As Runtime System in NullReferenceException | 更新日期: 2023-09-27 18:35:33

我有一个.NET 4.0 Web项目,并使用Microsoft.Bcl.Async来支持async/await功能。我注意到在生产环境中,由于在System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1()抛出的 NullReferenceException,IIS 工作进程不断崩溃。似乎异常在我的代码中的其他地方被抛出,并在AsyncServices.ThrowAsync()方法中重新抛出。不幸的是,任何异常详细信息(包括原始堆栈跟踪)都丢失了,所以我不知道异常最初在哪里抛出。此外,我尝试使用 AppDomain.CurrentDomain.UnhandledException 和 TaskScheduler.UnobservedTaskException 事件捕获此异常,但这些事件从未触发。

我有所有错误详细信息(来自事件日志):

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace:    at System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

我的代码中没有明显的地方可能会引发此异常并在没有任何输出的情况下进程崩溃。

如何调试?有没有办法找到此异常的来源?

NullReferenceException in System.Runtime.CompilerServices.As

不能在 IIS 上使用Microsoft.Bcl.Async;它完全不受支持。不好意思。

从引用的博客文章中总结一下:除非将 aspnet:UseTaskFriendlySynchronizationContext 应用程序设置设置为 true 或将 httpRuntime.targetFramework 属性设置为 4.5,否则 async/await 的行为在 ASP.NET 中是未定义的

这些选项不会影响 ASP.NET 4 运行时。