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'?
5 Answers
There are actually Roslyn analyzers that can help you detect when async methods aren't awaited. They're pretty helpful for cases like yours.
If you don't use Visual Studio, consider using SonarQube for IDE, as it can help identify these missed awaits.
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.
Check out a package called Lindhart analyzer. I tackled a similar issue recently and it worked wonders for catching unawaited tasks.
Are you using Entity Framework? It usually manages connections for you, so it might catch these kinds of issues automatically.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String