184

I stumbled on an issue (https://github.com/HTBox/allReady/issues/1313) at GitHub where they discussed about taking the ConfigureAwait(false) out of the code, claiming that, in ASP.NET Core

the call to ConfigureAwait(false) is redundant and does nothing

Best I could find here is a “side note” in an answer (from Stephen Cleary, https://stackoverflow.com/a/40220190/2805831) telling that

ASP.NET Core no longer has a "context"

So, is ConfigureAwait(false) really unnecessary in ASP.NET Core (even if using full .Net Framework)? Does it have any real gain in performance in some cases or difference in the result/semantic?

EDIT: Is it different in this aspect if I am hosting it as a console application or in IIS?

6
  • 9
    This depends on where you were planing to use it. If you wanted to use it directly in your ASP.NET Core application, then no you don't have to call it (you didn't had to call it in ASP.NET legacy neither iirc). But if you write a library, then you should always use ConfigureAwait(false), as the library can be consumed by different applications (ASP.NET Core, WPF, UWP, Console etc.) Commented Feb 6, 2017 at 0:08
  • 1
    ASP.NET Core runs as a console application by default, and AFAIK console applications don't have a SynchronizationContext, so yes, this sounds reasonable for a default ASP.NET Core application, even with the full Framework. Commented Feb 6, 2017 at 0:13
  • @JoeWhite Ok, edited the question. Is it different if my ASP.NET Core app is in IIS? Commented Feb 6, 2017 at 10:20
  • 3
    An ASP.NET Core app running in IIS still runs as a console application -- the only difference is that IIS is starting up and shutting down instances of your app (the same way it would have managed instances of the ASP.NET worker process in classic ASP.NET). It wouldn't change any thread-related behavior inside your ASP.NET app. (The only reason I specified "by default" is that you could, for example, host ASP.NET Core inside a GUI app, and in that case you would have to think about the synchronization context.) Commented Feb 6, 2017 at 14:04
  • 1
    Note ConfigureAwait(false), while relevant in ASP.NET classic, is by no means necessary. It's a tradeoff: it kinda mitigates some sync-over-async deadlocks (which are design flaws anyway--they don't exist unless someone does something dumb) and occasionally has a ~microsecond performance boost by not reloading context. At the cost of not being able to depend on context, and having ConfigureAwait all through your code. stackoverflow.com/questions/28221508/… Commented Mar 20, 2018 at 17:17

2 Answers 2

199

ConfigureAwait only has effects on code running in the context of a SynchronizationContext which ASP.NET Core doesn't have (ASP.NET "Legacy" does).

General purpose code should still use it because it might be running with a SynchronizationContext.

ASP.NET Core SynchronizationContext

ConfigureAwait FAQ

Sign up to request clarification or add additional context in comments.

7 Comments

Just want to put a slight clarification, ASP.NET in a non core environment does have a sync context, but ASP.NET core does not.
An ASP.NET Core app is not hosted in IIS. IIS is acting just as a reverse proxy.
I've updated the answer with a recent post from Stephen Cleary. But, yes, ASP.NET Core is ASP.NET Core.
@NamNgo. Appreciate this is an old post now but Stephen Cleary clarifies this in one of the questions on the post that Paulo linked above. "it's the framework (ASP.NET Core as opposed to ASP.NET Classic) that determines the SynchronizationContext, not the runtime (.NET Core as opposed to .NET 4.6.2)"
The question is clearly about ASP.NET Core, but I would add that even the app using the framework may theoretically create and configure its own SynchronizationContext, or a weird library may do so. If you are the app's developer, you should know about any such things, and likely, no such craziness is in the app. If you are a library developer, you should use ConfigureAwait even if the library is meant to be used with ASP.NET Core, or document that its use with a synchronization context is not supported.
|
0

One thing that is often not mentioned are TaskSchedulers. While it is true that ASP.NET Core/.NET 5+ stuff does not have a SynchronizationContext, it is still entirely possible to interact with different TaskSchedulers. Uncommon? Sure. But possible? Absolutely.

So do keep in mind that if you are using or interacting with a custom TaskScheduler, and say, for example, that TaskScheduler has its own thread pool, then ConfigureAwait(false) "breaks you out" of that TaskScheduler and its thread pool and throws your continuations back onto the default .NET thread pool.

I've had to deal with this extensively while I've been developing my .NET job orchestrator, Didact. Hangfire also has a custom TaskScheduler with its own thread pool, though Hangfire ends up using synchronous blocks on async code in its internal engine, so it's not as much of an issue there.

My point: just don't forget that ConfigureAwait(false) also affects TaskSchedulers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.