question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for IAsyncEnumerable<T>

See original GitHub issue

Is your feature request related to a problem? Please describe.

The API for enumerating query results isn’t great, because it forces the user to be aware of the fact that the results are fetched page by page:

var iterator = ...
while (iterator.HasMoreResults)
{
    var page = await iterator.ReadNextAsync();
    foreach (var item in page)
    {
        ...
    }
}

Describe the solution you’d like

The FeedIterator<T> API is useful, because it gives you more information (e.g. request charge), but in most cases the user just wants the results, so it would be nice to have a higher level abstraction. The IAsyncEnumerable<T> interface seems perfect for this.

There could be an AsAsyncEnumerable extension method like this:

public static async IAsyncEnumerable<T> AsAsyncEnumerable<T>(
    this FeedIterator<T> iterator,
    [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
    while (iterator.HasMoreResults)
    {
        var page = await iterator.ReadNextAsync(cancellationToken);
        foreach (var item in page)
        {
            cancellationToken.ThrowIfCancellationRequested();
            yield return item;
        }
    }
}

The consuming code would be simplified to this:

var iterator = ...
await foreach (var item in iterator.AsAsyncEnumerable())
{
    ...
}

Describe alternatives you’ve considered N/A

Additional context

The await foreach feature was introduced in C# 8, so this would only be useful to people already using C# 8. However, the Microsoft.Bcl.AsyncInterfaces package is compatible with netstandard2.0 and net461, so it wouldn’t require running .NET Core 3.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
ealsurcommented, Oct 14, 2019

We are working on this at the moment 😄

2reactions
paulirwincommented, Jun 16, 2021

Is there an anticipated release date for the v4 SDK? It looks like the last preview came out almost a year and a half ago. Thanks for any updates!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add support for `IAsyncEnumerable<T>` · Issue #1213
To assert on an IAsyncEnumerable<T> materialize the async enumerable into an IEnumerable<T> and use the existing assertions on enumerables. For ...
Read more >
System.Text.Json IAsyncEnumerable serialization - .NET
System.Text.Json now supports serializing and deserializing of IAsyncEnumerable<T> instances. Previous behavior. In previous versions, System.
Read more >
IAsyncEnumerable with yield in C# - Code Maze
Let's learn about IAsyncEnumerable and the way to iterate over an IEnumerable collection asynchronously with the yield keyword.
Read more >
Async Streams with IAsyncEnumerable<T> in .NET Core 3
It exposes an enumerator that has a MoveNextAsync() method that can awaited. This means the producer can make asynchronous calls in between ...
Read more >
ASP.NET Core 6 and IAsyncEnumerable - Async Streamed ...
This post goes a little bit into details of async streaming which is coming to ASP.NET Core in .NET 6 and shows how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found