在实现接口时删除异步/等待警告CS1998

发布于 2025-02-12 18:32:00 字数 856 浏览 0 评论 0原文

假设我实现了有两种方法的接口:创建createAsync。在我的情况下,异步方法不是真正异步,它使用Syncronous 创建方法返回创建对象:

public Task<GrandeurDTO> CreateAsync()
{
    var dto = Create();
    return Task.FromResult(dto);
}
public GrandeurDTO Create()
{
    var bo = new Grandeur();

    var dto = _mapper.Map<GrandeurDTO>(bo);
    return dto;
}

我应该如何删除警告

这种异步方法缺乏“等待”运算符,并且会同步运行。 考虑使用“等待”操作员等待非阻止API调用, 或'等待任务。lun(CS1998)

知道我不能在create中重命名该方法(我已经有一个;原因;如果不是一个问题,则与这个

Suppose I implement an interface where there are two methods: Create and CreateAsync. The Async method in my case is not really async, and it uses the syncronous Create method to return the created object:

public Task<GrandeurDTO> CreateAsync()
{
    var dto = Create();
    return Task.FromResult(dto);
}
public GrandeurDTO Create()
{
    var bo = new Grandeur();

    var dto = _mapper.Map<GrandeurDTO>(bo);
    return dto;
}

How should I remove the warning

This async method lacks 'await' operators and will run synchronously.
Consider using the 'await' operator to await non-blocking API calls,
or 'await Task.Run(CS1998)

knowing I can't rename the method in Create (I already have one; cause if not the question is similar to this one)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-19 18:32:00

我相信这只是一个疲倦的视觉工作室的情况,1)重新编译或2)与重新启动可以解决该问题。

I believe this is just a case of a tired Visual Studio and 1) a recompile or 2) VS restart will fix the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文