Crash at System.Web.LegacyAspNetSynchronizationContext.CallCallback

This post has been republished via RSS; it originally appeared at: IIS Support Blog articles.

If you see crash at the following call stack, that mostly probably is because web application is currently configured to use Legacy ASP.NET Synchronization Context, but the code is using some new features, such as Tasks (from Task Parallel Library) that require usage of what we call “Task-friendly ASP.NET Synchronization Context.” The old (Legacy) context cannot handle some asynchronous calls properly and this leads to the crash as observed in the above.

 

Web App Crashes frequently. Below are found in application event log each time of crash:

Application: w3wp.exe

Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception.

Exception Info: System.NullReferenceException

at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean)

at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean)

at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(System.Threading.SendOrPostCallback, System.Object)

at System.Web.LegacyAspNetSynchronizationContext.CallCallback(System.Threading.SendOrPostCallback, System.Object)

at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)

at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)

at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

 

 

To Resolve the issue add the following in web.config:

 

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.