Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException in System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1()

I have a .NET 4.0 web project and use Microsoft.Bcl.Async to support async/await feature. I've noticed that in production environment IIS worker process continiously crashes due to NullReferenceException which is thrown at System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1(). It seems that the exception is thrown some place else in my code and is rethrown at AsyncServices.ThrowAsync() method. Unfortunately any exception details (including original stack trace) are lost so I don't know where the exception has been thrown originally. Moreover, I've tried to catch this exception using AppDomain.CurrentDomain.UnhandledException and TaskScheduler.UnobservedTaskException events but these event are never fired.

There are all error details I have (from EventLog):

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

There are no obvious places in my code that may throw this exception and process crashes without any output.

How do I debug this? Are there any ways to find the source of this exception?

like image 860
Albert Avatar asked Oct 16 '13 14:10

Albert


1 Answers

You cannot use Microsoft.Bcl.Async on IIS; it's entirely unsupported. Sorry.

To summarize from the referenced blog post: the behavior of async / await is undefined in ASP.NET unless the aspnet:UseTaskFriendlySynchronizationContext app setting is set to true or the httpRuntime.targetFramework attribute is set to 4.5.

And those options don't affect the ASP.NET 4 runtime.

like image 190
Stephen Cleary Avatar answered Nov 07 '22 04:11

Stephen Cleary