How to return ActionResult along with async foreach and IAsyncEnumerable

2.6k Views Asked by At

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.

1

There are 1 best solutions below

0
George Jordanov On

this should work

    public ActionResult<IAsyncEnumerable<MyDto>> Get()
    {
        if(IsRequestInvalid())
        {
            // now can do.
            return Unauthorized();
        }

        return new ActionResult<IAsyncEnumerable<MyDto>>(DoSomeProcessing());

        IAsyncEnumerable<MyDto> DoSomeProcessing()
        {
            IAsyncEnumerable<MyDto> retrievedDtos = _someService.GetAllDtosAsync(_userId);

            await foreach(var currentDto in retrievedDtos)
            {
                //work with currentDto here

                yield return currentDto;
            }
        }
    }

if there is no processing of items before returning them better:

public ActionResult<IAsyncEnumerable<MyDto>> Get()
    {
        if(IsRequestInvalid())
        {
            // now can do
            return Unauthorized();
        }

        return new ActionResult<IAsyncEnumerable<MyDto>>(_someService.GetAllDtosAsync(_userId));
    }