I have a controller method of this signature:
public async IAsyncEnumerable<MyDto> Get()
It works fine but I need to do some request validation and return 401, 400, and other codes accordingly, which it does not support. Alternatively, the following signature does not compile:
public async Task<ActionResult<IAsyncEnumerable<MyDto>>> Get()
Error:
Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.UnauthorizedResult' to 'MyApi.Responses.MyDto'
The full method:
public async IAsyncEnumerable<MyDto> Get()
{
if (IsRequestInvalid())
{
// Can't do the following. Does not compile.
yield return Unauthorized();
}
var retrievedDtos = _someService.GetAllDtosAsync(_userId);
await foreach (var currentDto in retrievedDtos)
{
yield return currentDto;
}
}
Any ideas? Can't seem to believe that Microsoft has designed IAsyncEnumerable
to be used without the possibility/flexibility of returning anything else.