Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find missing await in solution

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.

like image 406
Frode Avatar asked Nov 30 '22 09:11

Frode


2 Answers

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.

like image 69
Charles Mager Avatar answered Dec 05 '22 00:12

Charles Mager


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

like image 41
gallivantor Avatar answered Dec 05 '22 00:12

gallivantor