I’ve been using IAsyncEnumerable recently for the streaming of SQL query rows and of deserialized objects from API calls and while it seems to work like a charm it does seem to have a few configuration issues.
Firstly, on creating a new .NET Core project in Visual Studio 2019 (v16.3.5) my projects seem to default to using version 7.3 of C# rather than the later version 8.0 which is the one where IAsyncEnumerable was introduced, this produces the following error.
CS8370: Feature ‘async streams’ is not available in C# 7.3. Please use language version 8.0 or greater.
This can be fixed by specifying that the latest installed version of C# should be used for the project by adding
<PropertyGroup> in the csproj file.
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> <LangVersion>latest</LangVersion> </PropertyGroup>
The other issue that I’ve noticed is that IAsyncEnumerable seems to exist in 2 namespaces at once in .NET Core 3+ as per this GitHub issue which produces the following error.
The type ‘IAsyncEnumerable’ exists in both ‘System.Interactive.Async, Version=22.214.171.124, Culture=neutral, PublicKeyToken=94bc3704cddfc263’ and ‘System.Runtime, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’
The solution to this is to remove the reference to IAsyncEnumerable existing in System.Interactive.Async as mentioned in the final comment in the above issue by adding the following code to your csproj file.
<Target Name="AddAssemblyAliasToReactiveAsync" AfterTargets="ResolveAssemblyReferences"> <ItemGroup> <ReferencePath Condition=" '%(FileName)' == 'System.Interactive.Async' "> <Aliases>reactive</Aliases> </ReferencePath> </ItemGroup> </Target>