From b5bfaf6e238f1b8b7f7dbe114c9d9f229183fe47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Wed, 22 Nov 2023 12:04:45 +0100 Subject: [PATCH] Avoid buffering the content before streaming to the response body --- src/Marten.AspNetCore/QueryableExtensions.cs | 22 +++----------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/Marten.AspNetCore/QueryableExtensions.cs b/src/Marten.AspNetCore/QueryableExtensions.cs index b971c743f1..1e0f4251d3 100644 --- a/src/Marten.AspNetCore/QueryableExtensions.cs +++ b/src/Marten.AspNetCore/QueryableExtensions.cs @@ -59,15 +59,10 @@ public static async Task WriteArray( int onFoundStatus = 200 ) { - var stream = new MemoryStream(); - await queryable.StreamJsonArray(stream, context.RequestAborted).ConfigureAwait(false); - context.Response.StatusCode = onFoundStatus; - context.Response.ContentLength = stream.Length; context.Response.ContentType = contentType; - stream.Position = 0; - await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false); + await queryable.StreamJsonArray(context.Response.Body, context.RequestAborted).ConfigureAwait(false); } /// @@ -266,15 +261,10 @@ public static async Task WriteArray( int onFoundStatus = 200 ) { - var stream = new MemoryStream(); - await session.StreamJsonMany(query, stream, context.RequestAborted).ConfigureAwait(false); - context.Response.StatusCode = onFoundStatus; - context.Response.ContentLength = stream.Length; context.Response.ContentType = contentType; - stream.Position = 0; - await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false); + await session.StreamJsonMany(query, context.Response.Body, context.RequestAborted).ConfigureAwait(false); } /// @@ -285,7 +275,6 @@ public static async Task WriteArray( /// /// /// Defaults to 200 - /// public static async Task WriteJson( this IQuerySession session, string sql, @@ -295,14 +284,9 @@ public static async Task WriteJson( params object[] parameters ) { - var stream = new MemoryStream(); - _ = await session.StreamJson(stream, context.RequestAborted, sql, parameters).ConfigureAwait(false); - context.Response.StatusCode = onFoundStatus; - context.Response.ContentLength = stream.Length; context.Response.ContentType = contentType; - stream.Position = 0; - await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false); + await session.StreamJson(context.Response.Body, context.RequestAborted, sql, parameters).ConfigureAwait(false); } }