英文:
'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
英文:
How to solve this issue ?
Service class:
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
};
}
}
Interface definition
public IAsyncEnumerable<UserDTO> GetAllUsers();
Controller
private readonly IUserService _userService;
public async IAsyncEnumerable<UserDTO> 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<T>
。await
返回一个单一的实例,而IAsyncEnumerable<T>
是一个流。
你可以像GetAllUsers
一样await foreach
一个IAsyncEnumerable<T>
,然后每次yield return
一个。但在这种情况下,你已经有了IAsyncEnumerable<T>
实例,所以直接返回它更有意义:
public IAsyncEnumerable<UserDTO> GetAllUsers()
{
return _userService.GetAllUsers();
}
英文:
You can't await
an IAsyncEnumerable<T>
. await
returns a single instance, while IAsyncEnumerable<T>
is a stream.
You can await foreach
an IAsyncEnumerable<T>
, just like GetAllUsers
does, and yield return
each one. But in this case, you already have the IAsyncEnumerable<T>
instance, so it makes more sense to just return it directly:
public IAsyncEnumerable<UserDTO> GetAllUsers()
{
return _userService.GetAllUsers();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论