-->

Find missing await in solution

2019-05-06 04:26发布

问题:

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.

回答1:

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.



回答2:

The easiest way to find them all is just compile the code.

Any function that has async but no await inside of it will cause compiler warning CS4014, once you compile your project look through the list of warnings and you will see all of the functions you need to fix.



回答3:

If the method doesn't contains even one await you may check the output window. After compilation you'll find This async method lacks 'await' operators and will run synchronously ... warning. Double click on that warning will take you to the relevant method.



回答4:

True you'll get the warning BUT if your app works without waiting those functions maybe you don't want to await them, just assign the resulting task to a variable to turn off the warning the guys told you about.

Obviously this is up to you to decide.