Home > Software engineering >  ASP.NET Core stream an IAsyncEnumerate on nested property
ASP.NET Core stream an IAsyncEnumerate on nested property

Time:07-24

I have read about returning an IAsyncEnumerate<Entity> to take advantage of HTTP streaming. This works if I return an IAsyncEnumerate<Entity> as top level. so, in the client:

Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
IAsyncEnumerable<Entity> entities = JsonSerializer.DeserializeAsyncEnumerable<Entity>(
    responseStream,
    new JsonSerializerOptions
    {
        PropertyNameCaseInsensitive = true,
        DefaultBufferSize = 128
    });

await foreach (Entity entity in entities)
{
    ...
}

works as expected.

In my case, my server returns a class where the IAsyncEnumerate<Entity> is a nested property:

public class ServerResponse
{
    public int Property1 {get; set;}
    public IAsyncEnumerate<Entity> Entities {get; set;}
}

In this case, can I get the advantage of streaming on client side?

CodePudding user response:

From the Docs

Wraps the UTF-8 encoded text into an IAsyncEnumerable that can be used to deserialize root-level JSON arrays in a streaming manner.

So no, as it is not root level, it unfortunately will not work.

  • Related