问题描述
在我们当前的项目中,我们使用依赖注入并使用async-await模式。这导致我们出现以下误用:
On our current project we are using dependency injection and utilizing the async-await pattern. This leads us to the following misuse:
我们有接口,其中方法返回一个Task(由于它在接口上,因此无法声明为异步)。然后在同步方法中使用这些方法之一而不等待该方法(因为开发人员没有注意到它实际上返回了
一个Task)。这导致了一种不必要的行为,其中该方法被用作火灾和遗忘 - 您希望它按顺序运行。
We have interfaces where methods return a Task (can't be declared async since it is on an interface). Then in a synchronous method one of these methods are used without awaiting the method (because the developer didn't notice that it actually returned a Task). This leads to a unwanted behavior where the method is used as fire and forget - where you'd want it to be run sequentially.
现在修复很简单,我们可以简单地进行调用方法异步并等待任务 - 问题是识别它何时发生,因为没有编译器警告和错误出现。
Now the fix is easy, we can simply make the calling method async and await the task - the problem is to identify when it happens since no compiler warnings nor errors appear.
推荐答案
这篇关于接口方法返回未等待的任务 - 没有警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!