As the lead for a C# library shared by desktop apps and ASP.NET Core services, what ConfigureAwait rule would you set for the team, and how would you enforce it?
answer
- library code vs app code
- the caller's context is unknown
- one missed await is enough
- an analyzer can count them
- no sync wrappers to block on
basics
~20 sA common rule: library code uses ConfigureAwait(false) on every await, enforced by analyzer CA2007 on library projects only; app code keeps the default; the library ships no sync-over-async wrappers. The costs are noise and missed awaits.
solid answer
~50 sI would separate library from application code. The shared library cannot know whether its caller has a UI `SynchronizationContext`, so it should not depend on one: every await gets `ConfigureAwait(false)`, including `await using` and `await foreach`, unless a method genuinely needs the caller's context. I would enforce it with analyzer CA2007 raised to a warning or error in the library's `.editorconfig`, scoped with its `output_kind` option or per project so UI apps are not told to break their own UI code. App code keeps bare awaits: a UI handler wants to come back, and ASP.NET Core has no context, so the call is noise there. The rule is a guard, not a licence to block, so the library exposes no synchronous wrappers. The costs are visual noise, a rule that is only as good as its weakest await, and a tempting but false sense that blocking is now safe.
go deeper
Know that shared library code and app code follow different ConfigureAwait habits, and that the library one is usually ConfigureAwait(false).
Explain why a library cannot rely on its caller's context, and why UI app code keeps the default.
Show the enforcement: CA2007 scoped to library projects, a test under a single-threaded context, and no sync-over-async wrappers.
Own the trade-off: readability and enforcement cost against deadlock risk from unknown callers, and keep the separate rule that UI code never blocks.
## The decision a lead has to make A shared C# library, say a reporting client, is consumed by a WPF desktop app and by ASP.NET Core services. Someone files a bug: the desktop app freezes when it calls the library and blocks with `.Result`. Someone else points out that the services never see this. The team needs one rule for `ConfigureAwait` that everyone can follow and a tool can check. There is no single right answer, but there are defensible positions with known costs. ## The options | Rule | Protects against | Costs | |---|---|---| | `ConfigureAwait(false)` on every await in library code | callers that block on a UI thread; needless posts to a UI context | noise on every await; one missed await keeps the bug alive | | No `ConfigureAwait` anywhere | nothing; relies on callers never blocking | desktop callers who block will deadlock; continuations hop to the UI thread needlessly | | `ConfigureAwait(false)` everywhere, app code included | the same as the first row | breaks UI code that touches controls after an await; pure noise in ASP.NET Core apps | | Only at the library's public entry points | partially, if each entry point's first suspension is configured | fragile: helpers that await before it still capture | Microsoft's guidance for library authors is the first row: use `ConfigureAwait(false)` on all awaits unless the code needs the captured context. The CA2007 documentation adds the other half: it is generally appropriate to suppress the rule for application code, and in an ASP.NET Core application `ConfigureAwait` does not change any behaviour. ## The rule I would set 1. **Library projects:** every `await` uses `ConfigureAwait(false)`. That covers `Task`, `Task<TResult>`, `ValueTask`, `await using` and `await foreach`, each of which has a `ConfigureAwait(bool)`. A method that really needs the caller's context, which is rare in a library, documents why and suppresses the warning locally. 2. **Application projects:** bare `await` by default. UI handlers rely on returning to the UI thread; ASP.NET Core has no context to skip. 3. **No synchronous wrappers in the library.** A `LoadReport()` that calls `LoadReportAsync().Result` hides the decision to block from the only party who knows the threading environment. If a synchronous API is truly required, it gets its own synchronous implementation or is clearly labelled and tested. 4. **Blocking stays forbidden in UI code**, rule or no rule. `ConfigureAwait(false)` in the library makes a blocked UI thread survivable; it does not make a frozen window acceptable. ## Enforcement - **Analyzer CA2007** ("Consider calling ConfigureAwait on the awaited task") is off by default. Turn it on for library projects with `dotnet_diagnostic.CA2007.severity = warning` (or `error` in CI) in their `.editorconfig`. It flags bare awaits, including `await using`. - **Scope it.** The rule's `dotnet_code_quality.CA2007.output_kind` option can limit it to `DynamicallyLinkedLibrary`, and `exclude_async_void_methods` can skip `async void` methods such as event handlers. Placing the setting in the library folder's `.editorconfig` works as well. - **Test from a context.** Add a test that runs the library's public methods under a single-threaded `SynchronizationContext` and blocks on them with a timeout. A missed `ConfigureAwait(false)` shows up as a timeout instead of a customer's frozen window. - **Review checklist:** new awaits in library code, any `.Result`, `.Wait()` or `GetAwaiter().GetResult()` outside tests, any new synchronous wrapper. ## Trade-offs to state out loud - **Noise and fatigue.** Hundreds of `.ConfigureAwait(false)` calls make code harder to read. The analyzer keeps them consistent; it cannot make them pretty. - **One missed await is enough.** The protection is only complete when no await that suspends on the caller's context lacks it. That is why enforcement must be mechanical, not a convention. - **Third-party code.** If the library calls a dependency while still on the caller's thread, and that dependency awaits without `ConfigureAwait(false)`, the library's rule cannot fix that; offloading that call with `Task.Run` is the fallback, at the cost of a thread hop. - **A false sense of safety.** The rule can be misread as "blocking is fine now". Keep the no-blocking rule for UI code explicit. - **Ambient data is unaffected.** `AsyncLocal<T>` values and other `ExecutionContext` state still flow, so logging scopes and correlation IDs do not break; the only thing the library gives up is a guaranteed thread, which it should not need. ## What a strong answer sounds like It names the library/app split and the reason (the library cannot know its caller's context), picks an enforcement mechanism (CA2007 scoped to libraries, a context-based test), refuses sync-over-async wrappers, and is honest that the rule reduces risk rather than removing the need to stay async all the way.
- Why would you not turn on CA2007 for a C# WPF or Windows Forms application project?In UI app code, the code after an await usually updates controls and must run on the UI thread, which the default capture provides. The CA2007 documentation itself says running it on application code, such as click handlers, is likely to lead to the wrong actions being taken. Applying it there would push developers to add `ConfigureAwait(false)` that breaks UI access.
- Does a library-wide ConfigureAwait(false) rule mean a desktop caller may now block on the library with .Result?It removes the deadlock but not the freeze: the UI thread is still blocked for the whole operation and cannot repaint. And any dependency the library awaits without the setting before its first suspension can still capture the UI context. The rule for UI code stays: await, do not block.
- Every await in every project, UI apps included, should use ConfigureAwait(false).
- ConfigureAwait(false) in the library makes blocking with .Result safe for UI callers.
- Putting ConfigureAwait(false) on the public method's first await is enough.
- ASP.NET Core services need ConfigureAwait(false) to avoid the UI-style deadlock.
- CA2007 is on by default, so no configuration is needed.