1

In ASP.NET (classic), when configuring an await to be ConfigureAwait(false) the default context is used to resume execution after the await instead of AspNetSynchronizationContext.

According to this article (table two thirds down) AspNetSynchronizationContext is implemented to execute one continuation at a time. The default SynchronizationContext executes continuations in parallel, potentially in more than one thread at a time.

So my question is, based on this information, is it true that applying ConfigureAwait(false) can potentially cause continuations to run in parallel, occupying multiple threads at a time for a single http request?

As an aside, after reading this article on SynchronizationContext in ASP.NET Core, Stephen warns of the implicit parallelism because there is no AspNetSynchronizationContext.

Sio
  • 1,419
  • 1
  • 13
  • 23
  • 1
    Seems like you already answered question by referencing Stephen Cleary's article ;) – Fabio Oct 04 '17 at 12:36
  • True! :) I just need some confirmation that this is what is happening. I can't see it discussed anywhere and the article is related to ASP.NET Core, not classic. It seems like a big deal to me that a single http request can occupy multiple threads in parallel when using ConfigureAwait(false) but it's not discussed anywhere. – Sio Oct 04 '17 at 12:38
  • As mentioned `ConfigureAwait(false)` should be used only in libraries, where you doesn't care about on which thread/context execution will be continued. In ASP.NET code you do not use it, so execution continues in one context/thread and you don't need to worry about parallelism – Fabio Oct 04 '17 at 12:45
  • But we do use it in our ASP.NET projects and I know lots of projects that also do. So what is happening here? I've never read anything warning not to use ConfigureAwait(false) in ASPNET. – Sio Oct 04 '17 at 12:49
  • From there : [Async/Await - Best Practices in Asynchronous Programming](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx) _You should not use ConfigureAwait when you have code after the await in the method that needs the context. For ASP.NET apps, this includes any code that uses HttpContext.Current or builds an ASP.NET response, including return statements in controller actions._ – Fabio Oct 04 '17 at 14:53

0 Answers0