How to Find Unawaited Async Method Calls in C#?

April 16, 2025 5 answers
Asked by CleverCoder93
I'm modernizing a legacy C# application and recently updated several SQL calls to use 'await'. However, I found an async method that wasn't awaited, which caused the main thread to close the database connection while it was still trying to execute some SQL. This resulted in an error due to the connection not being open. Is there a way in C# or Visual Studio to identify all instances where async methods are called without 'await'?

Answers

EFExpert55
Are you using Entity Framework? It usually manages connections for you, so it might catch these kinds of issues automatically.
Reply to this answer
LindhartGuru
Check out a package called Lindhart analyzer. I tackled a similar issue recently and it worked wonders for catching unawaited tasks.
Reply to this answer
DevWizard42
Not every async call has to be awaited, though. Sometimes just returning the task is more efficient and saves some overhead, but be aware it could lead to worse stack traces.
Reply to this answer
NoVisualStudioUser
If you don't use Visual Studio, consider using SonarQube for IDE, as it can help identify these missed awaits.
Reply to this answer
RoslynFan88
There are actually Roslyn analyzers that can help you detect when async methods aren't awaited. They're pretty helpful for cases like yours.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions