question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add analyzer to force ConfigureAwait(false) on all awaits

See original GitHub issue

We 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Mar 23, 2021

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:

  • Published as NuGet packages
  • IsAspNetCoreApp is false

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

0reactions
pranavkmcommented, Feb 22, 2022

I’m going to consider this resolved based on https://github.com/dotnet/aspnetcore/pull/39946.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found