Core SynchronizationContext
Core SynchronizationContext
David Pine (http://davidpine.net/) pinged me on Slack the other day, suggesting that I write a blog post about the SynchronizationContext in ASP.NET Core.
So, here it is!
(/assets/NoAspNetCoreSyncCtx.png)
Why No SynchronizationContext?
Stepping back a moment, a good question to ask is why the AspNetSynchronizationContext was removed in ASP.NET Core. While I’m not privy to the team’s
internal discussions on the subject, I assume it is for two reasons: performance and simplicity. Consider the performance aspect first.
When an asynchronous handler resumes execution on legacy ASP.NET, the continuation is queued to the request context. The continuation must wait for
any other continuations that have already been queued (only one may run at a time). When it is ready to run, a thread is taken from the thread pool, enters
the request context, and then resumes executing the handler. That “re-entering” the request context involves a number of housekeeping tasks, such as
setting HttpContext.Current and the current thread’s identity and culture.
With the contextless ASP.NET Core approach, when an asynchronous handler resumes execution, a thread is taken from the thread pool and executes the
continuation. The context queue is avoided, and there is no “entering” of the request context necessary. In addition, the async / await mechanism is highly
optimized for the contextless scenario. There’s simply less work to do for asynchronous requests.
Simplicity is another aspect of this decision. AspNetSynchronizationContext worked well, but it had some tricky parts, particularly around identity
management
(http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx).
OK, so there’s no SynchronizationContext . What does that mean for developers?
https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html 1/2
11/18/24, 5:13 PM ASP.NET Core SynchronizationContext
parallel.
As a contrived example, consider this code, which downloads two strings and places them into a list. This code works fine in legacy ASP.NET because the
request context only permits one continuation at a time:
The result.Add(data) line can only be executed by one thread at a time because it executes in the request context.
However, this same code is unsafe on ASP.NET Core; specifically, the result.Add(data) line may be executed by two threads at the same time, without
protecting the shared List<string> .
Code such as this is rare; asynchronous code is by its nature functional, so it’s far more natural to return results from asynchronous methods rather than
modifying shared state. However, the quality of asynchronous code does vary, and there is doubtless some code out there that is not adequately shielded
against parallel execution.
https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html 2/2