[英]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:如果您想稍后在同一方法中使用异步操作的结果,您将使用
async
和await
:
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.