查找解决方案中的缺失等待项 [英] Find missing await in solution

查看:98
本文介绍了查找解决方案中的缺失等待项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器代码中有很多异步方法,但是我怀疑我的呼叫者没有等待.

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);
}

那我忘了使用await的地方;

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.

推荐答案

这应显示为编译器警告CS4014,并显示以下文本:

This should be shown as compiler warning CS4014 with the text:

由于不等待此调用,因此在调用完成之前将继续执行当前方法.考虑将'await'运算符应用于调用结果.

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.

请参见文档

因此,您所需要做的就是编译代码. VS应该允许您从其错误列表中导航到相关位置.

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.

这篇关于查找解决方案中的缺失等待项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆