I was playing around with C# 8.0 preview and can't get IAsyncEnumerable
to work.
I tried the following
public static async IAsyncEnumerable<int> Get()
{
for(int i=0; i<10; i++)
{
await Task.Delay(100);
yield return i;
}
}
I ended up using a Nuget package named AsyncEnumerator
, but I'm getting the following error:
- Error CS1061 '
IAsyncEnumerable<int>
' does not contain a definition for 'GetAwaiter
' and no accessible extension method 'GetAwaiter
' accepting a first argument of type 'IAsyncEnumerable<int>
' could be found (are you missing a using directive or an assembly reference?)
- Error CS1624 The body of '
Program.Get()
' cannot be an iterator block because 'IAsyncEnumerable<int>
' is not an iterator interface type
What am I missing here?
That's a bug in the compiler that can be fixed by adding a few lines of code found here :
namespace System.Threading.Tasks
{
using System.Runtime.CompilerServices;
using System.Threading.Tasks.Sources;
internal struct ManualResetValueTaskSourceLogic<TResult>
{
private ManualResetValueTaskSourceCore<TResult> _core;
public ManualResetValueTaskSourceLogic(IStrongBox<ManualResetValueTaskSourceLogic<TResult>> parent) : this() { }
public short Version => _core.Version;
public TResult GetResult(short token) => _core.GetResult(token);
public ValueTaskSourceStatus GetStatus(short token) => _core.GetStatus(token);
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
public void Reset() => _core.Reset();
public void SetResult(TResult result) => _core.SetResult(result);
public void SetException(Exception error) => _core.SetException(error);
}
}
namespace System.Runtime.CompilerServices
{
internal interface IStrongBox<T> { ref T Value { get; } }
}
As Mads Torgersen explains in Take C# 8 for a spin :
But if you try compiling and running it, you get an embarassing number of errors. That’s because we messed up a bit, and didn’t get the previews of .NET Core 3.0 and Visual Studio 2019 perfectly aligned. Specifically, there’s an implementation type that async iterators leverage that’s different from what the compiler expects.
You can fix this by adding a separate source file to your project, containing this bridging code. Compile again, and everything should work just fine.
Update
Looks there's another bug when Enumerable.Range()
is used inside the async iterator.
The GetNumbersAsync()
method in the issue ends after only two iterations :
static async Task Main(string[] args)
{
await foreach (var num in GetNumbersAsync())
{
Console.WriteLine(num);
}
}
private static async IAsyncEnumerable<int> GetNumbersAsync()
{
var nums = Enumerable.Range(0, 10);
foreach (var num in nums)
{
await Task.Delay(100);
yield return num;
}
}
This will print only :
0
1
This won't happen with an array or even another iterator method:
private static async IAsyncEnumerable<int> GetNumbersAsync()
{
foreach (var num in counter(10))
{
await Task.Delay(100);
yield return num;
}
}
private static IEnumerable<int> counter(int count)
{
for(int i=0;i<count;i++)
{
yield return i;
}
}
This will print the expected :
0
1
2
3
4
5
6
7
8
9
Update 2
Seems that's a know bug as well:
Async-Streams: iteration stops early on Core
Regarding the bridging code needed to make Async enumerables work, I published a NuGet a couple of days ago that does just that: CSharp8Beta.AsyncIteratorPrerequisites.Unofficial
Contrary to popular belief, the following code actually produces the expected results:
private static async IAsyncEnumerable<int> GetNumbersAsync()
{
var nums = Enumerable.Range(0, 10).ToArray();
foreach (var num in nums)
{
await Task.Delay(100);
yield return num;
}
}
and that is because the IEnumerable<int>
is being materialized into an int
array. What would actually terminate after two iterations is iterating over the IEnumerable<int>
itself like so:
var nums = Enumerable.Range(0, 10); // no more .ToArray()
foreach (var num in nums) {
Still, while turning queries into materialized collections might seem like a clever trick, it isn't always the case that you would like to buffer the entire sequence (thus losing both memory and time).
With performance in mind, what I found is that an almost zero allocating wrapper over the IEnumerable
which would turn it into an IAsyncEnumerable
plus using await foreach
instead of just foreach
would circumvent the issue.
I have recently published a new version of the NuGet package which now includes an extension method called ToAsync<T>()
for IEnumerable<T>
in general, placed in System.Collections.Generic
which does just that. The method's signature is:
namespace System.Collections.Generic {
public static class EnumerableExtensions {
public static IAsyncEnumerable<T> ToAsync<T>(this IEnumerable<T> @this)
and upon adding the NuGet package to a .NET Core 3 project one could use it like so:
using System.Collections.Generic;
...
private static async IAsyncEnumerable<int> GetNumbersAsync() {
var nums = Enumerable.Range(0, 10);
await foreach (var num in nums.ToAsync()) {
await Task.Delay(100);
yield return num;
}
}
}
Notice the two changes:
foreach
becomes await foreach
nums
becoms nums.ToAsync()
The wrapper is as lightweight as possible and its implementation is based on the following classes (note that the using of ValueTask<T>
as enforced by the IAsyncEnumerable<T>
and IAsyncEnumerator<T>
allows for a constant number of Heap allocations per foreach
):
public static class EnumerableExtensions {
public static IAsyncEnumerable<T> ToAsync<T>(this IEnumerable<T> @this) => new EnumerableAdapter<T>(@this);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IAsyncEnumerator<T> ToAsync<T>(this IEnumerator<T> @this) => new EnumeratorAdapter<T>(@this);
private sealed class EnumerableAdapter<T> : IAsyncEnumerable<T> {
private readonly IEnumerable<T> target;
public EnumerableAdapter(IEnumerable<T> target) => this.target = target;
public IAsyncEnumerator<T> GetAsyncEnumerator() => this.target.GetEnumerator().ToAsync();
}
private sealed class EnumeratorAdapter<T> : IAsyncEnumerator<T> {
private readonly IEnumerator<T> enumerator;
public EnumeratorAdapter(IEnumerator<T> enumerator) => this.enumerator = enumerator;
public ValueTask<bool> MoveNextAsync() => new ValueTask<bool>(this.enumerator.MoveNext());
public T Current => this.enumerator.Current;
public ValueTask DisposeAsync() {
this.enumerator.Dispose();
return new ValueTask();
}
}
}
To sum it up:
To be able to write async generator methods ( async IAsyncEnumerable<int> MyMethod() ...
) and to consume async enumerables (await foreach (var x in ...
) simply install the
NuGet in your project.
In order to also circumvent the iteration premature stop, make sure you've got System.Collections.Generic
in your using
clauses, call .ToAsync()
on your IEnumerable
and turn your foreach
into an await foreach
.