我收到警告消息
此异步方法缺少“await”运算符,并将同步运行。考虑使用“await”运算符等待非阻塞 API 调用,或使用“await Task.Run(...)”在后台线程上执行 CPU 密集型工作。
产生此警告的代码是
app.MapGet("/questionsByCategory", async (Context dbContext, int categoryId, bool inTest) =>
{
IQueryable<Questions> result = dbContext.Questions
.AsNoTracking()
.Where(x => x.CategoryId == categoryId)
.Include(x => x.Answers)
.AsQueryable();
if (inTest)
result.Where(x => x.InTest == true);
return Results.Ok(result);
})
.WithName("questionsByCategory")
.WithOpenApi();
您可以在
.ToListAsync()
之后致电.AsQueryable()
。
更换
IQueryable<Questions> result = dbContext.Questions
.AsNoTracking()
.Where(x => x.CategoryId == categoryId)
.Include(x => x.Answers)
.AsQueryable();
与
List<Questions> result = await dbContext.Questions
.AsNoTracking()
.Where(x => x.CategoryId == categoryId)
.Include(x => x.Answers)
.AsQueryable()
.ToListAsync();