From 687b1ec581c6e1ab7481470eedf8e01c45ca3f9d Mon Sep 17 00:00:00 2001 From: Kendall Bennett Date: Thu, 31 Mar 2022 17:24:14 -0400 Subject: [PATCH] Always stream just the headers for DownloadStreamAsync for best memory utilization --- src/RestSharp/RestClient.Async.cs | 2 ++ src/RestSharp/RestClientExtensions.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index ed7b502fa..8eadd5bb3 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -95,6 +95,8 @@ record InternalResponse(HttpResponseMessage? ResponseMessage, Uri Url, Exception /// The downloaded stream. [PublicAPI] public async Task DownloadStreamAsync(RestRequest request, CancellationToken cancellationToken = default) { + // Make sure we only read the headers so we can stream the content body efficiently + request.CompletionOption = HttpCompletionOption.ResponseHeadersRead; var response = await ExecuteInternal(request, cancellationToken).ConfigureAwait(false); if (response.Exception != null) { diff --git a/src/RestSharp/RestClientExtensions.cs b/src/RestSharp/RestClientExtensions.cs index 501964d4c..22ac300da 100644 --- a/src/RestSharp/RestClientExtensions.cs +++ b/src/RestSharp/RestClientExtensions.cs @@ -331,7 +331,7 @@ public static async IAsyncEnumerable StreamJsonAsync( string resource, [EnumeratorCancellation] CancellationToken cancellationToken ) { - var request = new RestRequest(resource) { CompletionOption = HttpCompletionOption.ResponseHeadersRead }; + var request = new RestRequest(resource); #if NETSTANDARD using var stream = await client.DownloadStreamAsync(request, cancellationToken).ConfigureAwait(false);