How to return ActionResult along with async foreach and IAsyncEnumerable
Asked Answered
R

1

10

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.

Roswell answered 9/3, 2020 at 11:27 Comment(5)
This has little to do with IAsyncEnumerable. If you used async Task<MyDTO> you'd have the same problem. If you want to return specific responses, return IActionResult or ActionResult<T>Bung
This is explained in the docs: In such a case, it's common to mix an ActionResult return type with the primitive or complex return type. Either IActionResult or ActionResult<T> are necessary to accommodate this type of action.Bung
@PanagiotisKanavos It's not the same problem because in case of Task<MyDto>, I can easily make it Task<ActionResult<MyDto>> whereas I cannot do Task<ActionResult<IAsyncEnumerable<MyDto>>> (as mentioned in the question). And I need IAsyncEnumerable to pass the results to the serializer as they arrive.Roswell
It's exactly the same problem - unless you return an ActionResult or IActionResult, you can't return a status. The question is how to return that, and keep the benefits of IAsyncEnumerable. Looking at the source for ObjectResultExecutor, the class that actually sends object results, I see it has code to handle IAsyncEnumerableBung
You can try returning ActionResult<IAsyncEnumerable>, eg: return Ok(retrievedDtos).Bung
Y
0

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));
    }
Yearlong answered 29/5, 2020 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.