You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to accept very large JSON arrays. To avoid buffering the whole payload I read HttpRequest.Body directly and stream‑deserialize with JsonSerializer.DeserializeAsyncEnumerable<Data>().
That (hopefully) keeps memory usage low, but because the action has no [FromBody] parameter, the OpenAPI shows an empty request body.
[HttpPost][EndpointSummary("...")][EndpointDescription("...")][ProducesResponseType(StatusCodes.Status200OK,Type=typeof(int))][ProducesResponseType(StatusCodes.Status401Unauthorized,Type=typeof(string))][ProducesResponseType(StatusCodes.Status400BadRequest,Type=typeof(string))]publicasyncTask<ActionResult<int>>Post(CancellationTokencancellationToken){varprocessedCount=0;varjsonOptions=newJsonSerializerOptions{PropertyNameCaseInsensitive=true,};try{// The array can be a VERY LARGE array.awaitforeach(vardatainJsonSerializer.DeserializeAsyncEnumerable<Data>(Request.Body,jsonOptions,cancellationToken)){if(data==null){continue;}// send data to SQS / background worker hereprocessedCount++;}returnprocessedCount;}catch{returnBadRequest("Invalid request body");}}
Questions
How do I decorate the action so that OpenAPI documentation shows “array of Data in the request body” even though I’m reading Request.Body myself? (Is there an side-effect free attribute that you recommend? As far as my understanding if I use [FromBody] it will buffer and deserialize the content of the JSON into memory)
Is manual streaming still the right approach, or can MVC now bind directly to IAsyncEnumerable?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi ASP.NET Core team,
Scenario:
I need to accept very large JSON arrays. To avoid buffering the whole payload I read
HttpRequest.Body
directly and stream‑deserialize withJsonSerializer.DeserializeAsyncEnumerable<Data>()
.That (hopefully) keeps memory usage low, but because the action has no
[FromBody]
parameter, the OpenAPI shows an empty request body.Questions
How do I decorate the action so that OpenAPI documentation shows “array of Data in the request body” even though I’m reading
Request.Body
myself? (Is there an side-effect free attribute that you recommend? As far as my understanding if I use[FromBody]
it will buffer and deserialize the content of the JSON into memory)Is manual streaming still the right approach, or can MVC now bind directly to IAsyncEnumerable?
If this is (or will soon be) supported?
Related issues:
Any guidance or pointers to docs would be greatly appreciated.
Thanks for your time and for all the great work on ASP.NET Core!
Beta Was this translation helpful? Give feedback.
All reactions