'IAsyncEnumerable<T>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter'

huangapple go评论71阅读模式
英文:

'IAsyncEnumerable<T>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter'

问题

如何解决这个问题?

服务类:

public async IAsyncEnumerable<UserDTO> GetAllUsers()
{
    var allUsers = _userManager.Users.AsAsyncEnumerable();

    await foreach (IdentityUser user in allUsers)
    {
        yield return new UserDTO
        {
            Id = user.Id,
            UserName = user.UserName,
            Email = user.Email,
            Password = null
        };
    }
}

接口定义:

public IAsyncEnumerable<UserDTO> GetAllUsers();

控制器:

private readonly IUserService _userService;

public async IAsyncEnumerable<UserDTO> GetAllUsers()
{
    yield return await _userService.GetAllUsers();
}

控制器中的完整错误信息:await _userService.GetAllUsers()

错误信息:CS1061 'IAsyncEnumerable' 不包含 'GetAwaiter' 的定义,且没有可访问的扩展方法 'GetAwaiter',接受类型为 'IAsyncEnumerable' 的第一个参数的 (您是否缺少 using 指令或程序集引用?)

英文:

How to solve this issue ?

Service class:

public async IAsyncEnumerable&lt;UserDTO&gt; GetAllUsers()
{
    var allUsers= _userManager.Users.AsAsyncEnumerable();

    await foreach (IdentityUser user in allUsers)
    {
        yield return new UserDTO
        {
            Id = user.Id,
            UserName = user.UserName,
            Email = user.Email,
            Password = null
        };
    }
}

Interface definition

public IAsyncEnumerable&lt;UserDTO&gt; GetAllUsers();

Controller

private readonly IUserService _userService;

public async IAsyncEnumerable&lt;UserDTO&gt; GetAllUsers()
{
    yield return await _userService.GetAllUsers();
}

Full error in controller: await _userService.GetAllUsers()

> Error CS1061 'IAsyncEnumerable<UserDTO>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'IAsyncEnumerable<UserDTO>' could be found (are you missing a using directive or an assembly reference?)

答案1

得分: 1

你不能await一个IAsyncEnumerable&lt;T&gt;await返回一个单一的实例,而IAsyncEnumerable&lt;T&gt;是一个流。

你可以像GetAllUsers一样await foreach一个IAsyncEnumerable&lt;T&gt;,然后每次yield return一个。但在这种情况下,你已经有IAsyncEnumerable&lt;T&gt;实例,所以直接返回它更有意义:

public IAsyncEnumerable&lt;UserDTO&gt; GetAllUsers()
{
  return _userService.GetAllUsers();
}
英文:

You can't await an IAsyncEnumerable&lt;T&gt;. await returns a single instance, while IAsyncEnumerable&lt;T&gt; is a stream.

You can await foreach an IAsyncEnumerable&lt;T&gt;, just like GetAllUsers does, and yield return each one. But in this case, you already have the IAsyncEnumerable&lt;T&gt; instance, so it makes more sense to just return it directly:

public IAsyncEnumerable&lt;UserDTO&gt; GetAllUsers()
{
  return _userService.GetAllUsers();
}

huangapple
  • 本文由 发表于 2023年7月11日 02:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定