Akivamu
Akivamu

Reputation: 588

What replaces SynchronizationContext in ASP.NET Core?

ASP.NET Core doesn't have SynchronizationContext.

So how can the continuation of async code resume to where it begin (the original thread)?

Upvotes: 9

Views: 6417

Answers (1)

Eugene Chybisov
Eugene Chybisov

Reputation: 1704

TL;DR The continuation don't have to return to original thread.

Stephen Cleary has nice explanation to why ASP.NET Core doesn't have SynchronizationContext.

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.

Read more

I also recommend to dig into comments there are a lot of helpfull stuff out there.

Upvotes: 8

Related Questions