iasyncenumerable

How to await the results of an IAsyncEnumerable<Task<T>>, with a specific level of concurrency

此生再无相见时 提交于 2020-03-21 06:51:02
问题 I have an asynchronous stream of tasks, that is generated by applying an async lambda to a stream of items: IAsyncEnumerable<int> streamOfItems = AsyncEnumerable.Range(1, 10); IAsyncEnumerable<Task<string>> streamOfTasks = streamOfItems.Select(async x => { await Task.Delay(100); return x.ToString(); }) The methods AsyncEnumerable.Range and Select above are provided from the System.Linq.Async package. The result I want is a stream of results, expressed as an IAsyncEnumerable<string> . The

Using IAsyncEnumerable with Dapper

好久不见. 提交于 2020-03-05 02:51:06
问题 We have recently migrated our ASP.NET Core API which uses Dapper to .NET Core 3.1. After the migration, we felt there was an opportunity to use the latest IAsyncEnumerable feature from C# 8 for one of our endpoints. Here is the pseudocode before the changes: public async Task<IEnumerable<Item>> GetItems(int id) { var reader = await _connection.QueryMultipleAsync(getItemsSql, param: new { Id = id }); var idFromDb = (await reader.ReadAsync<int?>().ConfigureAwait(false)).SingleOrDefault(); if

How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

百般思念 提交于 2020-01-24 09:35:06
问题 I am trying to update my toolset with the new tools offered by C# 8, and one method that seems particularly useful is a version of Task.WhenAll that returns an IAsyncEnumerable. This method should stream the task results as soon as they become available, so naming it WhenAll doesn't make much sense. WhenEach sounds more appropriate. The signature of the method is: public static IAsyncEnumerable<TResult> WhenEach<TResult>(Task<TResult>[] tasks); This method could be used like this: var tasks =

Create empty IAsyncEnumerable

你说的曾经没有我的故事 提交于 2020-01-23 04:18:06
问题 I have an interface which is written like this: public interface IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync(); } I want to write an empty implementation that returns no item, like so: public class EmptyItemRetriever : IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync() { // What do I put here if nothing is to be done? } } If it was a plain IEnumerable, I would return Enumerable.Empty<string>(); , but I didn't find any AsyncEnumerable.Empty<string>() .

IAsyncEnumerable not working in C# 8.0 preview

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:54:24
问题 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 not working in C# 8.0 preview

柔情痞子 提交于 2019-12-04 03:15:57
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

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

懵懂的女人 提交于 2019-11-26 07:28:54
问题 Are regular iterator blocks (i.e. \"yield return\") incompatible with \"async\" and \"await\"? This gives a good idea of what I\'m trying to do: async Task<IEnumerable<Foo>> Method(String [] Strs) { // I want to compose the single result to the final result, so I use the SelectMany var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url\'s html code ); return