0

In an ASP.NET Core WebApi Application I succesfully used IAsyncEnumerable<T> as return type by the controller. Now I'd like to know if(and how) i can use IAsyncEnumerable<T> as input parameter. Something like this :

[Route("Operation")]
[HttpPost]
public async Task Operation(IAsyncEnumerable<Entity> commands)
{
        await foreach (Entity command in commands)
        {
            // do something
        }
}

I searched some articles but I found only articles on how to return it.

Is there a way to use IAsyncEnumerable<T> as an input parameter?

GLuca74
  • 105
  • 6
  • The controller action will not be called before whole request is processed and parameters deserialized. So there is no need of IAsyncEnumerable because all data is already loaded. – TcKs Jul 31 '22 at 10:33
  • Since the list of command may be long, I hoped there is a way to start process the command list without wait whole list is deserializated in memory. – GLuca74 Jul 31 '22 at 10:36
  • Afaik it is possible only through manual parsing body. https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-6.0#avoid-reading-large-request-bodies-or-response-bodies-into-memory – TcKs Jul 31 '22 at 10:46
  • sorry, i couldnt found how manual parsing body in the link you sent – GLuca74 Jul 31 '22 at 11:45
  • If you are using Asp.Net Core 6.0 and you are available to use [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json?view=net-6.0) then while formatting using System.Text.Json, MVC no longer buffers IAsyncEnumerable instances. You could see the detailed information in [this link](https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/6.0/iasyncenumerable-not-buffered-by-mvc) – Deepak-MSFT Aug 01 '22 at 08:07

0 Answers0