Questions tagged [iasyncenumerable]

IAsyncEnumerable exposes an IAsyncEnumerator enumerator that provides asynchronous iteration over values of type T

IAsyncEnumerable<T> was introduced as part of asynchronous streams in C# 8, which allows to retrieve and generate elements asynchronously. You can consume an async stream using an await foreach loop in the same way as you consume any sequence using a foreach loop

163 questions
4
votes
2 answers

Parallel receiving data from several IAsyncEnumerable streams

I have a case when I need to receive data from more than one IAsyncEnumerable source. For performance benefit it should be performed in parallel manner. I have written such code to achieve this goal using AsyncAwaitBestPractices,…
4
votes
3 answers

How to merge multiple asynchronous sequences without left-side bias?

I have a few AsyncEnumerables that I would like to merge in a single AsyncEnumerable, which should contain all the elements that are emitted concurrently from those sequences. So I used the Merge operator from the…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
4
votes
3 answers

How to query two IAsyncEnumerables asynchronously

I have two methods connected to two different sources of Foos which return two IAsyncEnumerable. I need to fetch all Foos from both sources before being able to process them . Problem : I would like to query both sources simultaneously…
XavierAM
  • 1,627
  • 1
  • 14
  • 30
4
votes
2 answers

Is it OK to cache IAsyncEnumerable for multiple consumers?

I am examining the effects of replacing some instances of regular C# event pattern with IAsyncEnumerable. This would be accomplished by lazy instantiation/activation of an IAsyncEnumerable and caching that reference for use by all callers/listeners.…
KyleP
  • 334
  • 2
  • 12
4
votes
1 answer

Is there a C# class like Queue that implements IAsyncEnumerable?

Both Queue and ConcurrentQueue implement IEnumerable but not IAsyncEnumerable. Is there a standard class or class available on NuGet which implements IAsyncEnumerable such that, if the queue is empty, the result of MoveNextAsync does not complete…
Julian Birch
  • 2,605
  • 1
  • 21
  • 36
4
votes
1 answer

Alternative to yielding in an IAsyncEnumerable

I'm trying to retrieve data that is paginated using the new AsyncEnumerables in C# 8.0. Back in the synchronous IEnumerable world the code would look something like this: private IEnumerable Example(S3FilesRequest requestData) { …
JoeS
  • 1,405
  • 17
  • 30
3
votes
2 answers

Read only first item from IAsyncEnumerable, then cancel

When consuming IAsyncEnumerable, is it possible to just read first item from the stream and then cancel the operation and return it? Something like a FirstOrDefault on IEnumerable? Cheers
Branislav B.
  • 509
  • 7
  • 21
3
votes
1 answer

How does cancellation work for IAsyncEnumerable.ToArrayAsync()?

I working with AsyncEnumerables to read a bunch of files into my C# application (.NET 6) and want to add an option to cancel reading. However I'm confused by the behaviour of ToArrayAsync when provided with a cancellationToken. I tried to break it…
3
votes
1 answer

Can I yield IAsyncEnumerable values as a list of Tasks complete?

Lets say I have a List that has 10 Tasks that each call a rest API. I'd like to run the tasks in a method that returns an IAsyncEnumerable that yields as each call is returned. I don't know what order the calls will return in. I've looked into…
FeeFiFoFum
  • 1,719
  • 1
  • 11
  • 18
3
votes
2 answers

Buffering IAsyncEnumerable in producer/consumer scenario

I Have a scenario in which I am reading some data from a database. This data is returned in the form of IAsyncEnumerable. After reading the data I want to send it to a consumer. This consumer is asynchronous. Right now my code looks…
Rafael
  • 182
  • 1
  • 9
3
votes
4 answers

From IEnumerable> to IAsyncEnumerable by yield returning inside a Parallel.ForEach/Parallel.ForEachAsync gives error CS1621

In a .NET 6 project, I have to call a web API which is offset paginated (page/per page) and I would like to make the n calls parallel as far as possible. This is the method which calls the API one time with the given page number: private…
3
votes
1 answer

Deserializing to AsyncEnumerable using Newtonsoft.Json

System.Text.Json.JsonSerializer.DeserializeAsyncEnumerable is a method by System.Text.Json that can take a Stream and produce an IAsyncEnumerable, where the enumeration can be asynchronous. This is useful for example, to deserialize an array…
Ohad
  • 183
  • 3
  • 12
3
votes
1 answer

How to lazily partition an IAsyncEnumerable?

I have an IAsyncEnumerable that returns what is essentially a sequence of Key/IEnumerable pairs. I have code consuming this and other similar enumerables, that assumes it will be receiving a unique collection of keys. But one of my data…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
3
votes
0 answers

EF Core difference between AsEnumerable and AsAsyncEnumerable

Are there any differences in behavior or performance between AsEnumerable, AsAsyncEnumerable and simple iteration over IQueryable in EF Core? // Using .AsEnumerable(); var blogs = context.Posts.Where(p =>…
3
votes
1 answer

How can I run a async enumerator method synchronously and store it as an IEnumerable?

I have an async method that returns an IAsyncEnumerable using yield. In some cases, it may be needed to get the items synchronously so I want to make another method for this that returns IEnumerable by running the existing method synchronously in…
TheBoxyBear
  • 371
  • 2
  • 15