Return IAsyncEnumerable from an async method

The struct approach wouldn’t work. If you want to asynchronously return an IAsyncEnumerator<T> value, you could use Task<IAsyncEnumerator<T>> with return Bar();. However, that would be unusual. It would be much more natural to create a new IAsyncEnumerator<T> that incorporates await SomeAsyncMethod() at the beginning of the asynchronous enumerable. To do this, you should use await … Read more

Create empty IAsyncEnumerable

If you install the System.Linq.Async package, you should be able to use AsyncEnumable.Empty<string>(). Here’s a complete example: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; class Program { static async Task Main() { IAsyncEnumerable<string> empty = AsyncEnumerable.Empty<string>(); var count = await empty.CountAsync(); Console.WriteLine(count); // Prints 0 } }

Linq methods for IAsyncEnumerable

LINQ for IAsyncEnumerable is supported by System.Linq.Async which is part of the reactive extensions for .NET. The reactive extensions as a whole are split into two larger NuGet packages: System.Reactive and System.Interactive. While all the packages stayed the same, the extensions now live in the System.Linq namespace, not System.Linq.Async anymore (thanks Dzmitry Lahoda). Relevant GitHub … Read more

How do you mock an IAsyncEnumerable?

I recommend using ToAsyncEnumerable from System.Linq.Async, as Jeroen suggested. It seems like you’re using Moq, so this would look like: async Task MyTest() { var mock = new Mock<MyService>(); var mockData = new[] { “first”, “second” }; mock.Setup(x => x.CallSomethingReturningAsyncStream()).Returns(mockData.ToAsyncEnumerable()); var sut = new SystemUnderTest(mock.Object); var result = await sut.MyMethodIWantToTest(); // TODO: verify `result` }

Convert IAsyncEnumerable to List

Sure – you just need the ToListAsync() method, which is in the System.Linq.Async NuGet package. Here’s a complete example: Project file: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”System.Linq.Async” Version=”4.0.0″ /> </ItemGroup> </Project> Code: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { IAsyncEnumerable<string> sequence = … Read more

Is it possible to “await yield return DoSomethingAsync()”

What you are describing can be accomplished with the Task.WhenAll method. Notice how the code turns into a simple one-liner. What happens is that each individual url begins downloading and then WhenAll is used combine those operations into a single Task which can be awaited. Task<IEnumerable<string>> DownLoadAllUrls(string[] urls) { return Task.WhenAll(from url in urls select … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)