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
7
votes
2 answers

Warning message in IAsyncEnumerable when it lacks the await operator

When calling a method like this without doing an await task, we can return the following: public Task GetBoolAsync() { return Task.FromResult(true); } What would be the equivalent for a IAsyncEnumerable<> and avoid the warning. async…
jsgoupil
  • 3,788
  • 3
  • 38
  • 53
7
votes
1 answer

Is it possible to transform an IObservable to an IAsyncEnumerable

I have an observable IObservable / ISubject and want to return that by using SignalR. SignalR has the concept of async streams where you have to return an IAsyncEnumerable. How can I transform an IObservable to an IAsyncEnumerable?
Rookian
  • 19,841
  • 28
  • 110
  • 180
6
votes
3 answers

Merge multiple IAsyncEnumerable streams

With the release of Mediatr 10, there's now a paradigm that allows developers to create streams powered by IAsyncEnumerable. I'm leveraging this paradigm to create multiple different file system watchers to monitor multiple folders. To monitor the…
JD Davis
  • 3,517
  • 4
  • 28
  • 61
6
votes
3 answers

Parallelize yield return inside C#8 IAsyncEnumerable

I have a method that returns an async enumerator public async IAsyncEnumerable DoWorkAsync() { await Something(); foreach (var item in ListOfWorkItems) { yield return DoWork(item); } …
Godsent
  • 950
  • 2
  • 8
  • 22
6
votes
1 answer

IAsyncEnumerable, yielding from event handler

I'm trying to wrap an asynchronous subscription API based on events with an API based on IAsyncEnumerable. Basically along the lines of: async IAsyncEnumerable ReadAll() { var reader = new EventBasedReader(); reader.OnRead => (_,…
Barguast
  • 5,926
  • 9
  • 43
  • 73
6
votes
1 answer

How to safely dispose of IAsyncDisposable objects retrieved with await foreach?

Can I do this using using var: await foreach (var response in _container.GetItemQueryStreamIterator(query)) { using var safeResponse = response; //use the safeResponse } or should I do this: await foreach (var response in…
fred_
  • 1,486
  • 1
  • 19
  • 31
5
votes
1 answer

What is the correct way to pass a cancellation token to an async stream?

For a while I've been trying to get my head around the whole async/await model that C# uses for asynchronous code. The addition of async streams (the IAsyncEnumerable type) seemed really cool, especially for some code that I was writing. Best…
Mike D.
  • 4,034
  • 2
  • 26
  • 41
5
votes
2 answers

Run enumeration of IAsyncEnumerable twice not possible?

Run enumeration of IAsyncEnumerable twice not possible? Once CountAsync has been run, the await foreach won't enumerate any item. Why? It seems there is no Reset method on the AsyncEnumerator. var count = await itemsToImport.CountAsync(); await…
CleanCoder
  • 2,406
  • 1
  • 10
  • 35
5
votes
2 answers

How to await multiple IAsyncEnumerable

We have code like this: var intList = new List{1,2,3}; var asyncEnumerables = intList.Select(Foo); private async IAsyncEnumerable Foo(int a) { while (true) { await Task.Delay(5000); yield return a; } } I need to start…
5
votes
1 answer

Try Catch using IAsyncEnumerable in SignalR ASP.NET Core 3.0

Trying to catch a top-level exception from an ASP.NET Core 3 SignalR Hub It is tricky as I'm using yield return, and you cannot wrap this in a try-catch block. It gives this compiler error: CS1626 C# Cannot yield a value in the body of a try block…
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
5
votes
6 answers

How to Return and Consume an IAsyncEnumerable with SqlDataReader

Please see the below two methods. The first returns an IAsyncEnumerable. The second tries to consume it. using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using…
user1325179
  • 1,535
  • 2
  • 19
  • 29
5
votes
2 answers

Iterating an IAsyncEnumerable in a function returning an IAsyncEnumerable with cancellation

As the title says, I have to following function: public async IAsyncEnumerable GetByPipeline(int pipelineId, [EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var job in context.Jobs.Where(job =>…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
5
votes
2 answers

C# 8 Async Streams vs REST/RPC

I'm sure this question is going to prove my ignorance, but I'm having a hard time understanding this. I'm willing to ask a dumb question to get a good answer. All of the posts I've read about async streams do a good job of showing off the feature,…
Alex Dresko
  • 5,179
  • 3
  • 37
  • 57
5
votes
4 answers

Is IAsyncEnumerable supported in C# 8.0?

Its really hard to find any information on IAsyncEnumerable, other than a few mentions in the 'What's New c# 8.0' articles. Trying to use it in Visual Studio 2019 with netstandard 2.0 and C# 8 enabled, it does recognize the class but i get a ton of…
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
5
votes
3 answers

IAsyncEnumerable<> broken in VS 2019 preview 2 (Core 3.0 preview 1)

After installing VS 2019 preview 2 i get a great number of errors. Error demo code: public class Class1 { public static async IAsyncEnumerable Get() { for( int i = 0; i < 10; i++ ) { await Task.Delay( 100 ); …
1 2
3
10 11