接口方法返回未等待的任务

编程入门 行业动态 更新时间:2024-10-26 05:34:44
本文介绍了接口方法返回未等待的任务 - 没有警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我们当前的项目中,我们使用依赖注入并使用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 asynchronous 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.

推荐答案

你的意思是 CS4014 ?

更多推荐

接口方法返回未等待的任务

本文发布于:2023-11-23 11:12:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!