Written by Mark Pringle | Last Updated on Wednesday, November 30, 2022

ASP.NET Core ASP.NET Version: 6.0 General Information

Asynchronous Streaming is a new feature in ASP.NET core 6 MVC that allows you to retrieve a collection of data or thousands of rows, etc., and stream them to a page or client asynchronously or without the bottleneck of buffering that data in memory.

The true benefit of asynchronous streaming is that you do not have to buffer a massive number of objects in memory before you can see them on the client or front end. 

Imagine that you are retrieving 100,000 rows of data. Without asynchronous streaming, all 100,000 rows of data would be fetched into memory first. Once all of the rows have been brought into memory, only then would the rows be displayed on the client. With asynchronous streaming, you would see each row shown on the client as it is retrieved. 

How Asynchronous Streaming is Implemented in an MVC Project

LearnASPNET.com has used asynchronous streaming to retrieve the articles displayed on the website's homepage. You can see by the following code how it is used in the controller and the view. We are using a stored procedure to retrieve the articles.

Controller

// GET: Home Page Articles
public async Task Index()
{
   var results = await _context.ArticleSelect.FromSqlInterpolated($"exec dbo.ArticlesHomePage").ToListAsync();
   return View(results);
}

View

Since I am using a view component on the home page representing each article snippet, the code to retrieve the component asynchronously will look like this:

@await Component.InvokeAsync("ArticleCard")