In this article, we will know about .NET6 feature that is MVC/API controller actions now supports the asynchronous streaming response.
Async Stream:
A data stream is often retrieved or generates elements asynchronously is 'Async Stream'. Now from .NET6 we can output the async stream of response from the controller's actions. An action method that contains return type 'IAsyncEnumerable' no longer buffers the response content in application memory before it sends to the clients. So not buffering help to reduce the application memory usage when returning large dataset result that can be asynchronously enumerated.
Sample Action Method That Returns 'IAyncEnumerable<T>' Results:
Let's write a sample code that can return the asynchronous stream of data.
[Route("Test")] [HttpGet] public async IAsyncEnumerable<int> GetNumbers() { for (int i = 1; i < 100; i++) { await Task.Delay(10); Console.WriteLine(i); yield return i; } }
- Here our logic is simple using 'IAsyncEnumerable', 'yield' we are returning value for every iteration.
- (Line: 7&8) The 10s delay and console logs were implemented to understand the Async Stream.
.NET5 Or Below Version Don't Support Async Stream Response:
.NET5 or below versions won't support the async stream response, but it has support for output formatting IAsyncEnumerable<> types by buffering the sequence in memory and formatting the buffered collection.
So now add the above action method in .NET5 API. So action method yields a value for every loop but that yielded value will be stored or buffered into the application memory, once entire results return from the action method then the results in the application memory gets formatted and then finally returns output to the user so from this we can understand it won't support the async stream of response.
Output from .NET5 application looks as below
.NET 6 Supports Async Stream Response:
The .NET6 supports the async stream of response. So in .NET6 application the 'IAsyncEnumerable<>' instances are no longer buffered by the MVC/API, that is because of the support provided by the 'System.Text.Json' formatted. So 'System.Text.Json' formater directly pushes the stream of response to the client without waiting for the entire result set from the action method.
Now add the above action method into the .NET6 application, since we have async stream response support on every iteration value will send to the client asynchronously.
Output from the .NET6 application looks as below.
Entity Framework Core Supports IAsyncEnumerable:
Entity Framework also supports the IAsyncEnumerable, so it can return a stream of response but there is a limitation if the query contains any lazy loading implementation then it will fallback to the buffer implementation where it will load all the data into application memory and then returns the results.
Sample Ef Code:
[Route("db")] [HttpGet] public async Task<IActionResult> GetDB() { return Ok(await _myWorldDbContext.Todo.ToListAsync()); }
- So this can return as the async stream of response if the query doesn't have any lazy loading properties.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful on the async stream of response in .NET6 application. I love to have your feedback, suggestions, and better techniques in the comment section below.
Comments
Post a Comment