Async Streaming
Overview
- ASP.NET Core now supports asynchronous streaming from controller actions and responses from the JSON formatter using
System.Text.Json
- Returning an
IAsyncEnumerable
from an action no longer buffers the response content in memory before it gets sent. Not buffering helps reduce memory usage when returning large datasets that can be asynchronously enumerated. - Entity Framework Core provides implementations of IAsyncEnumerable for querying the database. The improved support for IAsyncEnumerable make using EF Core with ASP.NET Core more efficient.
public IActionResult Get()
{
return Ok(dbContext.Blogs);
}
Manual Buffering
When using lazy loading in EF Core, this new behavior may result in errors due to concurrent query execution while the data is being enumerated. Consider manually buffering the async enumerable.
public async Task<IActionResult> Get()
{
return Ok(await dbContext.Blogs.ToListAsync());
}