Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of IAsyncEnumerable in .NET Core 3.0?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the use of IAsyncEnumerable in .NET Core 3.0". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of IAsyncEnumerable in .NET Core 3.0".

In this article, we will learn what challenges IAsyncEnumerable addresses, how to implement it in our own applications, and why IAsyncEnumerable will replace Task in many cases.

Perhaps the best proof, IAsyncEnumerable.

< T >

A useful way is to look at the difficulties you face without it.

For example, first of all, there is a code that gets all the favorite posts by page:

Public async Task GetPagePostsFromLikes (int pageNumber) {/ / implement ellipsis}

Then there is another piece of code that calls the above code:

Public async Task GetAllPostsFromLikes () {var allPosts = new List ()

For (int page = 0; page++) {var posts = await GetPagePostsFromLikes (page); if (! posts.Any ()) {return allPosts;} allPosts.AddRange (posts);}}

Note that there is a problem with the above method. We loop through the results of each page, put them into List, and return the entire result. Suppose there are hundreds of millions of posts for page pages, then all posts for hundreds of millions of page pages need to be loaded before the return value is returned. It's obviously very inefficient.

Maybe we can replace the above method without using Task:

Public IEnumerable GetAllPostsFromLikes () {for (int page = 0;; page++) {var posts = GetPagePostsFromLikes (page). GetAwaiter (). GetResult (); if (! posts.Any ()) {yield break } foreach (var post in posts) {yield return post;}

In the above code, the method that returns IEnumerable can use the yield return statement to return each piece of data to the caller.

But don't do it! The above code means that if we call the third function from an asynchronous method, the thread pool will continue to iterate over the returned IEnumerable until it is complete, which means that when there are enough concurrent accesses to the same thread, it is bound to cause blocking.

If only we could use yield return asynchronously! It's a pity that's impossible. up to now.

It's time for IAsyncEnumerable to come out!

IAsyncEnumerable was introduced in .NET Core 3 (.NET Standard 2.1). It exposes an enumerator with a MoveNextAsync () method that can wait. This means that producers can make asynchronous calls between producing results.

Unlike the return task, our method can now return IAsyncEnumerable and use yield return to send data:

Public async IAsyncEnumerable GetAllPostsFromLikes () {for (int page = 0;; page++) {var posts = GetPagePostsFromLikes (page). GetAwaiter (). GetResult (); if (! posts.Any ()) {yield break } foreach (var post in posts) {yield return post;}

To use the results, we need to use the new await foreach () syntax in c # 8:

Await foreach (var post in postsRepository.GetAllPostsFromLikes ()) {Console.WriteLine (post);}

This is much better. This method generates available data. The calling code uses the data at its own pace.

Since .NET Core 3.0 Preview 7, ASP.NET has been able to return IAsyncEnumerable from API controller actions, which means that we can directly return the results of the method-effectively streaming data from the database to the HTTP response.

[HttpGet] public IAsyncEnumerable Get () = > postsRepository.GetAllPostsFromLikes ()

Over time, as the .NET Core3.0 and .NET Standard2.1 evolve, we will see IAsyncEnumerable being used where we usually use Task.

Thank you for reading, the above is the content of "what is the use of IAsyncEnumerable in .NET Core 3.0". After the study of this article, I believe you have a deeper understanding of the use of IAsyncEnumerable in .NET Core 3.0, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report