ApiEndpoints icon indicating copy to clipboard operation
ApiEndpoints copied to clipboard

Add support for IAsyncEnumerable

Open DorianGreen opened this issue 2 years ago • 2 comments

Right now, in order to return an IAsyncEnumerable you need to use EndpointBaseSync and access the request cancellationToken form HttpContext like this:

public class List : EndpointBaseSync.WithoutRequest.WithResult<IAsyncEnumerable<PropertyListResult>>
{
    /// <summary>
    /// List all Properties
    /// </summary>
    [HttpGet("api/[namespace]")]
    public override async IAsyncEnumerable<PropertyListResult> Handle()
    {
        var cancellationToken = HttpContext.RequestAborted;
        await Task.CompletedTask;
        yield break;
    }
}

It would be nice if we could do something like this:

public class List : EndpointBaseAsync.WithoutRequest.WithAsyncEnumerableResult<PropertyListResult>
{
    /// <summary>
    /// List all Properties
    /// </summary>
    [HttpGet("api/[namespace]")]
    public override async IAsyncEnumerable<PropertyListResult> Handle(CancellationToken cancellationToken = default)
    {
        await Task.CompletedTask;
        yield break;
    }
}

DorianGreen avatar Mar 21 '22 17:03 DorianGreen

See also https://github.com/jbogard/MediatR/pull/574#issue-736342983 as a reference for the needed targetframework changes.

cmxl avatar Apr 02 '22 17:04 cmxl

#176 is waiting for merge. targetframework has been bumped to netcore3.1 with #180 and so no need to change targets anymore

cmxl avatar Jun 19 '22 20:06 cmxl

Feature is now in main. Issue can be closed <3

cmxl avatar Nov 30 '22 22:11 cmxl

is the only thing missing, a new nuget version?

AlexanderFinkbeiner avatar Jun 01 '23 13:06 AlexanderFinkbeiner

I guess it has been a while...

ardalis avatar Jun 01 '23 13:06 ardalis

are you sure? 4.0.1 does not include WithAsyncEnumerableResult image

AlexanderFinkbeiner avatar Jun 01 '23 13:06 AlexanderFinkbeiner