WebApi .Net Core 错误,因为不包含 GetAwaiter 的定义

MD *_*ohi 3 c# asp.net-core

我是 .NET Core 的新手,我正在阅读此文档https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1

从那里我正在练习 - 我写下这个逻辑:

public async Task<ActionResult<List<DashBoar>>> GetAllAsync()
{
    var x = _Repo.GetPetsAsync();
    return await x.ToList();
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误。

我的回购课程是

public IEnumerable<DashBoar> GetPetsAsync()
{
    var x = from n in _context.DashBoar
            select n;
    return  x.ToList();
}
Run Code Online (Sandbox Code Playgroud)

小智 7

首先你应该了解什么是异步编程以及await、async和Task的相互关系。

异步编程用于提高应用程序性能并增强响应能力。请参阅底部的链接以了解情况。

首先让我们解决您的问题。将您的存储库类返回类型设置为 Tak

public async Task<IEnumerable<DashBoar>> GetPetsAsync()
{
     var x = await (from n in _context.DashBoar
             select n).ToListAsync();

     return x;
}
Run Code Online (Sandbox Code Playgroud)

然后从 GetAllAsync() 方法调用 repo 方法,如下所示

public async Task<ActionResult<List<DashBoar>>> GetAllAsync()
{
     var x = await _Repo.GetPetsAsync();
     return x;
}
Run Code Online (Sandbox Code Playgroud)

请浏览以下链接以更好地了解异步编程。

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ https://www.youtube.com/watch?v=C5VhaxQWcpE

https://www.dotnetperls.com/async

祝你好运..!