Find non-awaited async method calls

前端 未结 5 1439
无人及你
无人及你 2020-12-24 11:36

I\'ve just stumbled across a rather dangerous scenario while migrating an ASP.NET application to the async/await model.

The situation is that I made a method async:

5条回答
  •  情书的邮戳
    2020-12-24 12:17

    The compiler will emit warning CS4014 but it is only emitted if the calling method is async.

    No warning:

    Task CallingMethod() {
        DoWhateverAsync();
        // More code that eventually returns a task.
    }
    

    Warning CS4014: 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.

    async Task CallingMethod() {
        DoWhateverAsync();
    }
    

    This is not terrible useful in your specific case because you have to find all the places where DoWhateverAsync is called and change them to get the warning and then fix the code. But you wanted to use the compiler warning to find these calls in the first place.

    I suggest that you use Visual Studio to find all usages of DoWhateverAsync. You will have to modify the surrounding code anyway either by going through compiler warnings or by working through a list of usages.

提交回复
热议问题