Search code examples
c#.netasynchronousasync-awaitroslyn-code-analysis

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.


Solution

  • 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.