简体   繁体   English

ASP.NET 内核 Web API - 如何解决警告 CS1998 此异步方法缺少“等待”运算符,将同步运行

[英]ASP.NET Core Web API -How to resolve Warning CS1998 This async method lacks 'await' operators and will run synchronously

I am implementing async programming in ASP.NET Core-6 Web API.我在 ASP.NET Core-6 Web API 中实现异步编程。

I have this code:我有这个代码:

public async Task<IQueryable<Score>> GetExamScoreAsync(PagingFilter filter)
{
    var examScores = _dbContext.Scores
            .Where(m => (bool)m.Approval.IsFirstLevel == false)
            .Where(x => string.IsNullOrEmpty(filter.SearchQuery)
        || x.Subject.ToLower().Contains(filter.SearchQuery.ToLower())
        || x.StartDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture).Contains(filter.SearchQuery.ToLower())
        || x.EndDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture).Contains(filter.SearchQuery.ToLower()))
            .OrderByDescending(x => x.CreatedAt);
    return examScores;
}

I got this warning:我收到了这个警告:

Severity Code Description Project File Line Suppression State Warning CS1998 This async method lacks 'await' operators and will run synchronously.严重性代码 描述 项目文件行抑制 State 警告 CS1998 此异步方法缺少“等待”运算符,将同步运行。 Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work考虑使用“await”运算符来等待非阻塞 API 调用,或“await Task.Run(...)”来执行 CPU 密集型工作

When I added await before _dbContext.Scores, I got this error:当我在 _dbContext.Scores 之前添加 await 时,出现此错误:

Error CS1061 'IOrderedQueryable' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'IOrderedQueryable' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“IOrderedQueryable”不包含“GetAwaiter”的定义,并且找不到接受“IOrderedQueryable”类型的第一个参数的可访问扩展方法“GetAwaiter”(您是否缺少 using 指令或程序集引用?)

How do I get this sorted out and apply async here?我该如何解决这个问题并在这里应用异步?

Thank you.谢谢你。

You're not calling an asynchronous method that returns a Task / Task<T> so there's nothing to await (and not the right type to return).您没有调用返回Task / Task<T>的异步方法,因此没有什么可等待的(并且返回的类型不正确)。 Update your method to what Artur suggested in their comment or make another call to generated the Task you're looking for (but you wouldn't need async and the result type wouldn't be IQueryable<T> . Some simplified possibilities:将您的方法更新为 Artur 在他们的评论中建议的内容,或再次调用生成您正在寻找的Task (但您不需要async并且结果类型不会是IQueryable<T> 。一些简化的可能性:

public IQueryable<MyEntity> Get(...)
{
    return dbContext.MyEntities
        .Where(...);
}

public IAsyncEnumerable<MyEntity> Get(...)
{
    return dbContext.MyEntities
        .Where(...)
        .AsAsyncEnumerable();
}

public Task<MyEntity[]> Get(...)
{
    return dbContext.MyEntities
        .Where(...)
        .ToArrayAsync();
}

You would use async and await if you want to use the result of an asynchronous operation later in the same method:如果您想稍后在同一方法中使用异步操作的结果,您将使用asyncawait

public async Task<int> GetCount(...)
{
    var entities = await dbContext.MyEntities
        .Where(...)
        .ToArrayAsync();

    // obviously not efficient but to demonstrate
    // how async/await is used
    return entities.Length;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Task.WaitAll 的使用没有任何“等待”运算符导致警告 CS1998 此异步方法缺少“等待”运算符,将同步运行 - Usage of Task.WaitAll without any 'await' operators causing warning CS1998 This async method lacks 'await' operators and will run synchronously 解决警告 CS1998 时如何处理异常:此异步方法缺少“等待”运算符 - How do I handle Exceptions when resolving warning CS1998: This async method lacks 'await' operators c# 异步等待奇怪的警告 CS1998:此异步方法缺少“等待”运算符 - c# async await strange warning CS1998: This async method lacks 'await' operators 抑制警告 CS1998:此异步方法缺少“等待” - Suppress warning CS1998: This async method lacks 'await' CS1998“方法缺乏等待运营商”背后的原因是什么? - What is the reason behind CS1998 “method lacks await operators” 警告 CS1998 这种异步方法缺少“等待”运算符,正确的静音方法是什么? - Warning CS1998 This async method lacks 'await' operator, correct way to silence? 如何删除StyleCop警告“此异步方法缺少&#39;await&#39;运算符,将同步运行”,而不会从签名中删除异步 - How to remove StyleCop warning “This async method lacks 'await' operators and will run synchronously” without removing async from signature 此异步方法缺少“等待”运算符,将同步运行 - This async method lacks 'await' operators and will run synchronously 在实现接口时删除 async/await 警告 CS1998 - Remove the async/await warning CS1998, when implementing interface 我是否应该担心“此异步方法缺少 &#39;await&#39; 运算符并将同步运行”警告 - Should I worry about "This async method lacks 'await' operators and will run synchronously" warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM