Add analyzer to force ConfigureAwait(false) on all awaits
See original GitHub issueWe don’t add ConfigureAwait(false)
to ASP.NET Core code because there is no sync context. Microsoft.Extensions.* code could execute in client apps. Not using ConfigureAwait(false)
can create deadlocks when a user mixes sync with async, e.g. https://github.com/aspnet/Extensions/issues/999.
Consider introducing an analyzer to enforce ConfigureAwait(false)
everywhere. A quick search of this repos source code displays many non-test awaits that don’t follow best practice.
I just had some good success using https://www.nuget.org/packages/ConfigureAwaitChecker.Analyzer/
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Set ConfigureAwait(false) for entire project/dll
@AbdulSaboor: These days there are code analyzers that can force you to always use ConfigureAwait (either true or false ), to make it...
Read more >ConfigureAwait analysis - JetBrains Rider
The ConfigureAwait analysis relies on two code inspections: Redundant 'ConfigureAwait(true)' in the UI mode and Missing '.ConfigureAwait(false)' ...
Read more >CA2007: Do not directly await a Task (code analysis) - .NET
To fix violations, call ConfigureAwait on the awaited Task. You can pass either true or false for the continueOnCapturedContext parameter.
Read more >ConfigureAwait FAQ - .NET Blog
ConfigureAwait (false) involves a task that's already completed by the time it's awaited (which is actually incredibly common), then the ...
Read more >Enforcing asynchronous code good practices using a ...
In this post, I describe the Roslyn Analyzers that help writing good async/await code in .NET.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t know what Andrew ment when he said this is mostly done. I don’t think we’ve done anything 😬
Basically I think we should turn on CA2007 for all our libraries that can run outside of ASP.NET Core. We’re writing code without
ConfigureAwait(false)
because we’re assuming that there is never a sync context, but some of our libraries can be used in WinForms or WPF. That’s created bugs (linked in initial comment).We should come up with a rule to apply the analyzer setting across the repo. Perhaps all projects that are:
Note that there are some projects where the rule will be turned off. For example, the .NET SignalR client has a different strategy to avoid sync context issues (@BrennanConroy can you confirm, its been a couple of years since I looked at the .NET SignalR client code).
tldr; DoI should pick this up
I’m going to consider this resolved based on https://github.com/dotnet/aspnetcore/pull/39946.