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:
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?)Program.Get()
' cannot be an iterator block because 'IAsyncEnumerable<int>
' is not an iterator interface typeWhat 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