{"id":308242,"date":"2020-08-10T15:00:14","date_gmt":"2020-08-10T15:00:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=308242"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-08-10T15:00:14","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=308242","title":{"rendered":"\u0427\u0442\u043e \u0436\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e \u0432 IAsyncEnumerable \u0432 .NET Core 3.0?"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/514594\/\"><b><i>\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/hNs7\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a C#\u00bb<\/a>.<\/i><br \/>  <\/b>  <\/p>\n<hr>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/az\/-n\/0x\/az-n0xmh5fbzjp0ifakfdvn0lzo.png\"><\/p>\n<p>  \u041e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 .NET Core 3.0 \u0438 C# 8.0 \u0441\u0442\u0430\u043b \u043d\u043e\u0432\u044b\u0439 <code><code>IAsyncEnumerable&lt;T&gt;<\/code><\/code> (\u043e\u043d \u0436\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a). \u041d\u043e \u0447\u0442\u043e \u0432 \u043d\u0435\u043c \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e? \u0427\u0442\u043e \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0435\u043f\u0435\u0440\u044c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0430\u043d\u044c\u0448\u0435?<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0440\u0435\u0448\u0430\u0442\u044c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043d\u0430\u0448\u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0437\u0430\u043c\u0435\u043d\u0438\u0442 <code><code>Task&lt;IEnumerable&lt;<\/code>T&gt;<code>&gt;<\/code><\/code> \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445.<\/p>\n<p>  \u041e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441\u043e \u0432\u0441\u0435\u043c\u0438 <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/whats-new\/dotnet-core-3-0?WT.mc_id=devto-blog-antchu\">\u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 .NET Core 3<\/a><a name=\"habracut\"><\/a><\/p>\n<h3>\u0416\u0438\u0437\u043d\u044c \u0434\u043e <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code><\/h3>\n<p>  \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0442\u0430\u043a \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u2014 \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u043e \u043d\u0435\u0433\u043e.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0434\u0430\u043d\u043d\u044b\u043c, \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0438\u043b\u0438 API. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code><code>Task&lt;<\/code>IEnumerable<code>&lt;<\/code>T&gt;&gt;<\/code>, \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c:<\/p>\n<pre><code class=\"cs\">public async Task&lt;IEnumerable&lt;Product&gt;&gt; GetAllProducts() <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434, \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u0445, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u044d\u0442\u0438\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0448\u0430 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 API \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0446\u0435\u043b\u044b\u043c\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438, \u043a\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0430\u044f Azure Cosmos DB:<\/p>\n<pre><code class=\"cs\">public async Task&lt;IEnumerable&lt;Product&gt;&gt; GetAllProducts() {     Container container = cosmosClient.GetContainer(DatabaseId, ContainerId);     var iterator = container.GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;);     var products = new List&lt;Product&gt;();     while (iterator.HasMoreResults)     {         foreach (var product in await iterator.ReadNextAsync())         {             products.Add(product);         }     }     return products; }<\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043f\u0440\u043e\u043b\u0438\u0441\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0446\u0438\u043a\u043b\u0435 while, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 product, \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0438\u0445 \u0432 List, \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0432\u0441\u0435 \u0446\u0435\u043b\u0438\u043a\u043e\u043c. \u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043e \u0446\u0435\u043b\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0437\u0430 \u0440\u0430\u0437:<\/p>\n<pre><code class=\"cs\">public IEnumerable&lt;Task&lt;IEnumerable&lt;Product&gt;&gt;&gt; GetAllProducts() {     Container container = cosmosClient.GetContainer(DatabaseId, ContainerId);     var iterator = container.GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;);     while (iterator.HasMoreResults)     {         yield return iterator.ReadNextAsync().ContinueWith(t =&gt;          {             return (IEnumerable&lt;Product&gt;)t.Result;         });     } }<\/code><\/pre>\n<p>  \u0412\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cs\">foreach (var productsTask in productsRepository.GetAllProducts()) {     foreach (var product in await productsTask)     {         Console.WriteLine(product.Name);     } }<\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430, \u043d\u043e \u043c\u0435\u0442\u043e\u0434 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code><code>IEnumerable&lt;<\/code>Task<code>&lt;<\/code>IEnumerable<code>&lt;<\/code>Product&gt;&gt;&gt;<\/code>. \u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0438\u0437 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b. \u0427\u0442\u043e \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u043e, \u043f\u043e\u0434\u043a\u0430\u0447\u043a\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u2014 \u044d\u0442\u043e \u0434\u0435\u0442\u0430\u043b\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u0437\u043d\u0430\u0442\u044c.<\/p>\n<h3><code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0441\u043f\u0435\u0448\u0438\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c<\/h3>\n<p>  \u0427\u0442\u043e \u0431\u044b \u043c\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u043e\u0442\u0435\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0442\u0430\u043a \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u043e \u043c\u0435\u0440\u0435 \u0438\u0445 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0412 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 IEnumerable, \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 yield return \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"cs\">public IEnumerable&lt;Product&gt; GetAllProducts() {     Container container = cosmosClient.GetContainer(DatabaseId, ContainerId);     var iterator = container.GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;);     while (iterator.HasMoreResults)     {         foreach (var product in iterator.ReadNextAsync().Result)         {             yield return product;         }     } }<\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, <i>\u041d\u0418\u041a\u041e\u0413\u0414\u0410 \u0422\u0410\u041a \u041d\u0415 \u0414\u0415\u041b\u0410\u0419\u0422\u0415<\/i>! \u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043a\u043e\u0434 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0438 \u043d\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>yield return<\/code> \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438! \u042d\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u2026 \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440.<\/p>\n<p>  <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0431\u044b\u043b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 .NET Core 3 (.NET Standard 2.1). \u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u044d\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>MoveNextAsync()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0432\u043e \u0432\u0440\u0435\u043c\u044f (\u043f\u043e\u0441\u0440\u0435\u0434\u0438) \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.<\/p>\n<p>  \u0412\u043c\u0435\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 Task<code>&lt;<\/code>IEnumerable<code>&lt;<\/code>T&gt;&gt; \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c yield return \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"cs\">public async IAsyncEnumerable&lt;Product&gt; GetAllProducts() {     Container container = cosmosClient.GetContainer(DatabaseId, ContainerId);     var iterator = container.GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;);     while (iterator.HasMoreResults)     {         foreach (var product in await iterator.ReadNextAsync())         {             yield return product;         }     } }<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 <code>await foreach()<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0432 C# 8:<\/p>\n<pre><code class=\"cs\">await foreach (var product in productsRepository.GetAllProducts()) {     Console.WriteLine(product); }<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u0435\u0435. \u041c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043c\u0435\u0440\u0435 \u0438\u0445 \u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044f. \u041a\u043e\u0434 \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u0432\u043e\u0435\u043c \u0442\u0435\u043c\u043f\u0435.<\/p>\n<h3><code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0438 ASP.NET Core<\/h3>\n<p>  \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 <a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-7\/?WT.mc_id=devto-blog-antchu\">.NET Core 3 Preview 7<\/a>, ASP.NET \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c IAsyncEnumerable \u0438\u0437 \u044d\u043a\u0448\u0435\u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 API. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u2014 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 HTTP \u043e\u0442\u0432\u0435\u0442.<\/p>\n<pre><code class=\"cs\">[HttpGet] public IAsyncEnumerable&lt;Product&gt; Get()     =&gt; productsRepository.GetAllProducts();<\/code><\/pre>\n<p>  <\/p>\n<h3>\u0417\u0430\u043c\u0435\u043d\u0430 <code><code>Task&lt;<\/code>IEnumerable<code>&lt;<\/code>T&gt;&gt;<\/code> \u043d\u0430 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code><\/h3>\n<p>  \u0421 \u0442\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e \u0445\u043e\u0434\u0443 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u044f .NET Core 3 \u0438 .NET Standard 2.1, \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043c\u0435\u0441\u0442\u0430\u0445, \u0433\u0434\u0435 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Task<code>&lt;<\/code>IEnumerable&gt;.<\/p>\n<p>  \u042f \u0441 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043d\u0438\u0435\u043c \u0436\u0434\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0432\u0438\u0434\u0435\u043b\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SDK Azure Cosmos DB 3.0:<\/p>\n<pre><code class=\"cs\">var iterator = container.GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;); while (iterator.HasMoreResults) {     foreach (var product in await iterator.ReadNextAsync())     {         Console.WriteLine(product.Name);     } }<\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0438 \u0432 \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 SDK Cosmos DB \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043d\u0430\u0441 \u0434\u0435\u0442\u0430\u043b\u044f\u043c\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u043a\u0430\u0447\u043a\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0447\u0442\u043e \u0437\u0430\u0442\u0440\u0443\u0434\u043d\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0433\u043b\u043e \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c, \u0435\u0441\u043b\u0438 \u0431\u044b <code><code>GetItemQueryIterator&lt;<\/code>Product&gt;()<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434-\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 <code>FeedIterator<\/code>:<\/p>\n<pre><code class=\"cs\">public static class FeedIteratorExtensions {     public static async IAsyncEnumerable&lt;T&gt; ToAsyncEnumerable&lt;T&gt;(this FeedIterator&lt;T&gt; iterator)     {         while (iterator.HasMoreResults)         {             foreach(var item in await iterator.ReadNextAsync())             {                 yield return item;             }         }     } }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0430\u0448\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c:<\/p>\n<pre><code class=\"java\">var products = container     .GetItemQueryIterator&lt;Product&gt;(&quot;SELECT * FROM c&quot;)     .ToAsyncEnumerable(); await foreach (var product in products) {     Console.WriteLine(product.Name); }<\/code><\/pre>\n<p>  <\/p>\n<h3>\u0420\u0435\u0437\u044e\u043c\u0435<\/h3>\n<p>  <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u2014 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043b\u0433\u043e\u0436\u0434\u0430\u043d\u043d\u044b\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u043a .NET \u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u043c \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c. \u0423\u0437\u043d\u0430\u0442\u044c \u043e\u0431 \u044d\u0442\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430 \u044d\u0442\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/tutorials\/generate-consume-asynchronous-stream?WT.mc_id=devto-blog-antchu\">Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/proposals\/csharp-8.0\/async-streams?WT.mc_id=devto-blog-antchu\">C# language proposals \u2014 Async Streams<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<hr>\n<p>  <a href=\"https:\/\/otus.pw\/hNs7\/\">\u0428\u0430\u0431\u043b\u043e\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u00ab\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state)\u00bb<\/a><\/p>\n<hr>\n<p>  <\/p>\n<h2>\u0427\u0438\u0442\u0430\u0442\u044c \u0435\u0449\u0451:<\/h2>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/502498\/\">\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 C#<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/500012\/\">Entity Framework Core<\/a><\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/514594\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/514594\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/514594\/\"><b><i>\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/hNs7\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a C#\u00bb<\/a>.<\/i><br \/>  <\/b>  <\/p>\n<hr>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/az\/-n\/0x\/az-n0xmh5fbzjp0ifakfdvn0lzo.png\"><\/p>\n<p>  \u041e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 .NET Core 3.0 \u0438 C# 8.0 \u0441\u0442\u0430\u043b \u043d\u043e\u0432\u044b\u0439 <code><code>IAsyncEnumerable&lt;T&gt;<\/code><\/code> (\u043e\u043d \u0436\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a). \u041d\u043e \u0447\u0442\u043e \u0432 \u043d\u0435\u043c \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e? \u0427\u0442\u043e \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0435\u043f\u0435\u0440\u044c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0430\u043d\u044c\u0448\u0435?<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0440\u0435\u0448\u0430\u0442\u044c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043d\u0430\u0448\u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 <code><code>IAsyncEnumerable&lt;<\/code>T&gt;<\/code> \u0437\u0430\u043c\u0435\u043d\u0438\u0442 <code><code>Task&lt;IEnumerable&lt;<\/code>T&gt;<code>&gt;<\/code><\/code> \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445.<\/p>\n<p>  \u041e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441\u043e \u0432\u0441\u0435\u043c\u0438 <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/whats-new\/dotnet-core-3-0?WT.mc_id=devto-blog-antchu\">\u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 .NET Core 3<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-308242","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=308242"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308242\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}