Announcements
Announcements copied to clipboard
Actions returning IAsyncEnumerable<> are no longer buffered by MVC when using System.Text.Json
Actions returning IAsyncEnumerable<> are no longer buffered by MVC when using System.Text.Json
In 5.0, MVC added support for output formatting IAsyncEnumerable<>
types by buffering the sequence in memory and formatting the buffered collection. In 6.0, when formatting using System.Text.Json, IAsyncEnumerable<>
instances are no longer buffered by MVC, instead relying on the support for these types added to System.Text.Json.
In most cases, the absence of buffering would not be observed by the application. However, some scenarios may have inadvertently relied on the buffering semantics to correctly serialize. For instance, returning an IAsyncEnumerable<>
that is backed by a EF query on a type with lazy loaded properties might result in concurrent query execution which might be unsupported by the provider.
This change does not affect output formatting using Newtonsoft.Json, or with XML-based formatters
Version introduced
6.0-preview4
Old behavior
IAsyncEnumerable<>
instances returned from an MVC action as a value to be formatted using ObjectResult
, or a JsonResult
would be buffered before being serialized as synchronous collection.
New behavior
When formatting using System.Text.Json, IAsyncEnumerable<>
instances are no longer buffered by MVC.
Reason for change
System.Text.Json added support for streaming IAsyncEnumerable<>
types. This allows for a smaller memory footprint during serialization.
Recommended action
If your application requires buffering, consider manually buffering the async enumerable:
// Before
public IActionResult Get()
{
return Ok(dbContext.Blogs);
}
// After
public async Task<IActionResult> Get()
{
return Ok(await dbContext.Blogs.ToListAsync());
}
Category
ASP.NET
Affected APIs
"Not detectable via API analysis"
Issue metadata
- Issue type: breaking-change
Please use https://github.com/dotnet/aspnetcore/issues/32483 for questions and further discussion.