Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions returning IAsyncEnumerable<> are no longer buffered by MVC when using System.Text.Json #463

Open
pranavkm opened this issue May 6, 2021 · 1 comment · Fixed by dotnet/docs#24264
Labels
6.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs
Milestone

Comments

@pranavkm
Copy link
Contributor

pranavkm commented May 6, 2021

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
@pranavkm
Copy link
Contributor Author

pranavkm commented May 6, 2021

Please use dotnet/aspnetcore#32483 for questions and further discussion.

@aspnet aspnet locked and limited conversation to collaborators May 6, 2021
@Rick-Anderson Rick-Anderson added the Documented The breaking change has been published to the .NET Core docs label May 17, 2021
@davidfowl davidfowl added this to the 6.0.0 milestone May 21, 2021
@gewarren gewarren added the 6.0.0 label Jul 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
6.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants