I have lots of async methods in my server code, but I suspect that I have callers without await.
Is there a simple way to scan the code for calls where await is missing?
public async Task DomeSomethingAsync()
{
var result = await GetResult();
await StoreResult(result);
}
then somewhere I've forgot to use await;
public async Task SomeBuggyCode()
{
await Initialize();
DoSomethingAsync(); // DOH - Forgot await
}
I was hoping there was a smart way to identitfy these erroronous calls.
This should be shown as compiler warning CS4014 with the text:
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
See the documentation
So all you should need to do is compile the code. VS should allow you to navigate to the relevant locations from its Error List.
The CS4014 warning will get generated if the missing "await" is to a method that the compiler knows is async.
It won't work if you miss an await when calling an async method via an interface or in an external library.
For that reason, it's worth installing this nuget package: https://www.nuget.org/packages/Lindhart.Analyser.MissingAwaitWarning/
this will raise a warning in any cases where a method that returns a Task is not awaited
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With