0

On legacy ASP.NET, the continuation of asynchronous method is queued to the request context (AspNetSynchronizationContext). But on ASP.NET Core, there is no such a SynchronizationContext (contextless).

So, where are asynchronous continuations queued in ASP.NET Core?

With this contextless condition, where is HttpContext information stored so that the threads from ThreadPool can refer to it?

tuq
  • 1,328
  • 2
  • 11
  • 21
  • 2
    The `HttpContext` is not stored centrally; it's stored in each controller class instance. – SLaks Dec 21 '18 at 16:31
  • @Slaks, could you please tell me more detail? I think there is a way for multiple threads use the HttpContext object, right? So, which is the exact thread which HttpContext object is stored? Thank you! – tuq Jan 04 '19 at 16:01
  • No such mechanism exists. Instead, you access the context from your class, which stores it in a field. – SLaks Jan 04 '19 at 16:49

1 Answers1

1

I highly recommend you to read Stephen Cleary blog, and even his book. In this post he talks about Synchronization Context in ASP.NET Core.

I highlight this paragraph:

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.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • Thanks @harkoded for the suggestion, I had read his post before. I just wonder how HttpContext could work in ASP.NET Core if it's serverless (_where is HttpContext information stored so that the threads from ThreadPool can refer to it?_). In legacy ASP.NET, we only can access to HttpContext object if we are on UI thread. Accessing to `System.Web.HttpContext.Current.Items["abc"]` will throw exception if we're not on UI thread. Refer to https://stackoverflow.com/questions/29268001/what-does-context-exactly-mean-in-c-sharp-async-await-code – tuq Dec 22 '18 at 04:33