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
142
votes
9 answers

Is it possible to "await yield return DoSomethingAsync()"

Are regular iterator blocks (i.e. "yield return") incompatible with "async" and "await"? This gives a good idea of what I'm trying to do: async Task> Method(String [] Strs) { // I want to compose the single result to the final…
jiangzhen
  • 1,542
  • 2
  • 10
  • 7
109
votes
2 answers

Convert IAsyncEnumerable to List

So in C#8 we got the addition of the IAsyncEnumerable interface. If we have a normal IEnumerable we can make a List or pretty much any other collection we want out of it. Thanks to Linq there. var range = Enumerable.Range(0, 100); var list =…
Twenty
  • 5,234
  • 4
  • 32
  • 67
50
votes
3 answers

Create empty IAsyncEnumerable

I have an interface which is written like this: public interface IItemRetriever { public IAsyncEnumerable GetItemsAsync(); } I want to write an empty implementation that returns no item, like so: public class EmptyItemRetriever :…
cube45
  • 3,429
  • 2
  • 24
  • 35
49
votes
4 answers

How do you mock an IAsyncEnumerable?

I want to unit test a method that calls another method of a service returning an IAsyncEnumerable. I have created a a mock of my service Mock and I want to setUp this mock but I don't know how to do that. Is it possible ? Are there…
TechWatching
  • 1,363
  • 1
  • 11
  • 23
43
votes
2 answers

Linq methods for IAsyncEnumerable

When working with an IEnumerable there are the build-in extension methods from the System.Linq namespace such as Skip, Where and Select to work with. When Microsoft added IAsyncEnumerable in C#8 did they also add new Linq methods to support…
GWigWam
  • 2,013
  • 4
  • 28
  • 34
30
votes
1 answer

Return IAsyncEnumerable from an async method

Take the following the methods: public async IAsyncEnumerable Foo() { await SomeAsyncMethod(); return Bar(); // Throws since you can not return values from iterators } public async IAsyncEnumerable Bar() { for(int i = 0; i < 10;…
Twenty
  • 5,234
  • 4
  • 32
  • 67
21
votes
2 answers

Clarification on how IAsyncEnumerable works with ASP.NET Web API

I encountered an interesting behavior while exploring IAsyncEnumerable in an ASP.NET Web API project. Consider the following code samples: // Code Sample 1 [HttpGet] public async IAsyncEnumerable GetAsync() { for (int i…
Ravi M Patel
  • 2,905
  • 2
  • 23
  • 32
20
votes
2 answers

How to await all results from an IAsyncEnumerable<>?

I'm tinkering around with the new IAsyncEnumerable stuff in C# 8.0. Let's say I've got some method somewhere that I want to consume: public IAsyncEnumerable SomeBlackBoxFunctionAsync(...) { ... } I'm aware that I can use it with the await…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
19
votes
2 answers

Using IAsyncEnumerable with Dapper

We have recently migrated our ASP.NET Core API which uses Dapper to .NET Core 3.1. After the migration, we felt there was an opportunity to use the latest IAsyncEnumerable feature from C# 8 for one of our endpoints. Here is the pseudocode before the…
Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
19
votes
2 answers

What's the difference between returning AsyncEnumerable with EnumeratorCancellation or looping WithCancellation

I have the following method that reads a csv document from a http stream public async IAsyncEnumerable GetLines([EnumeratorCancellation] CancellationToken cancellationToken) { HttpResponseMessage response = GetResponse(); using var…
Homulvas
  • 524
  • 5
  • 18
19
votes
1 answer

What is the correct way to use linq type methods with IAsyncEnumerable?

There does not seem to be any included linq support for IAsyncEnumerable packaged with .NET Core. What is the correct way to be able to do simple things such as ToList and Count?
cubesnyc
  • 1,385
  • 2
  • 15
  • 32
19
votes
2 answers

Can you use IAsyncEnumerable in Razor pages to progressively display markup?

I've been playing around with Blazor and the IAsyncEnumerable feature in C# 8.0. Is it possible to use IAsyncEnumerable and await within Razor Pages to progressively display markup with data? Example service: private static readonly string[] games =…
17
votes
2 answers

Why am I not allowed to return an IAsyncEnumerable in a method returning an IAsyncEnumerable

I have the following interface: public interface IValidationSystem { IAsyncEnumerable ValidateAsync(T obj); } And I am trying to implement it this way: public class Foo { } public class Bar { } public class…
fharreau
  • 2,105
  • 1
  • 23
  • 46
17
votes
3 answers

How to force an IAsyncEnumerable to respect a CancellationToken

I have an async iterator method that produces an IAsyncEnumerable (a stream of numbers), one number every 200 msec. The caller of this method consumes the stream, but wants to stop the enumeration after 1000 msec. So a CancellationTokenSource…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
16
votes
1 answer

How Async streams compares to reactive extension?

How to compare the following two? Is Rx more powerful? Reactive extension: var observable = Observable.Create(async (observer, cancel) => { while (true) { string line = await sr.ReadLineAsync(); if (line == null) …
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
1
2 3
10 11