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
{{ message }}
Repository navigation
How to get response content without storing it in MemoryStream #1794
Hello!
Before v107 i use DownloadData with AdvancedResponseWriter, and as i remember, it doesn't store ResponseContent in memory and allow to write something like this:
var request = new RestRequest(downloadUri, Method.GET);
request.AdvancedResponseWriter = (stream, response) =>
{
using var fStream = File.OpenWrite(filePath);
var buffer = new byte[1024*64];
long totalCount = 0;
while (totalCount != response.ContentLength)
{
var count = stream.Read(buffer, 0, buffer.Length);
totalCount += count;
fStream.Write(buffer, 0, count);
}
};
_client.DownloadData(request);
Now in v107.3, when i use DownloadDataAsync or just ExecuteAsync, it store all data in memory and only after that return me the MemoryStream.
How can i avoid storing ResponseContent in memory and write it in the file as is?
Because in my case, i need to downloading big files, which size can be a few gigabytes, and storing it in memory, on clients desktop PC - looks like a bad idea.
But the question was about DownloadStreamAsync .
Because when i use it, it store all data in memory, in MemoryStream, and only after that, it returns stream.
In first message, i ref a method from ResponseHandling, and if this method is used for DownloadAsyncStream, i dont now how it may work async as i want, without storing all in memory, because of - https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasstreamasync?view=net-6.0
The returned Task object will complete after all of the stream that represents content has been read.
It's returns stream only after all content was read.
Therefore, this means we have memory stream, that read all response in it, and only after that return to me this memory stream from DownloadStreamAsync method.
May be i do something wrong, and i would appreciate it if you could give an example of the correct usage.
What the referenced issue states is not exactly right. If you look at StreamJsonAsync, it uses DownloadDataAsync and continuously reads the stream. The stream content is available as soon as it gets something from the server.
I just ran into this also. Wouldn't it make more sense to always set that completion option for DownloadStreamAsync? Otherwise DownloadStreamAsync really is not much point, because it defeats the purpose of actually downloading into a stream?
Also that would make DownloadDataAsync be more efficient, as currently it will end up downloading the data first into a memory stream, which is then streamed into a byte[] array to return the caller. If it was actually streaming the response content, then the byte[] array is built more efficiently (internally it's streamed into a memory stream 16K at a time which is then turned into the byte array). So if we are downloading say a 2MB file, there will be a 2MB memory stream created by the DownloadStreamAsync() function, then another 2MB memory stream that the data gets copied into, which then gets turned into the final byte array.
Activity
alexeyzimarev commentedon Mar 16, 2022
Use
DownloadStreamAsynchttps://restsharp.dev/usage.html#downloading-binary-dataRacer396 commentedon Mar 16, 2022
But the question was about DownloadStreamAsync .
Because when i use it, it store all data in memory, in MemoryStream, and only after that, it returns stream.
In first message, i ref a method from ResponseHandling, and if this method is used for DownloadAsyncStream, i dont now how it may work async as i want, without storing all in memory, because of - https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasstreamasync?view=net-6.0
It's returns stream only after all content was read.
Therefore, this means we have memory stream, that read all response in it, and only after that return to me this memory stream from DownloadStreamAsync method.
May be i do something wrong, and i would appreciate it if you could give an example of the correct usage.
Racer396 commentedon Mar 16, 2022
And intresting discussion about that in dotnet repo: dotnet/runtime#31316
alexeyzimarev commentedon Mar 16, 2022
What the referenced issue states is not exactly right. If you look at
StreamJsonAsync, it usesDownloadDataAsyncand continuously reads the stream. The stream content is available as soon as it gets something from the server.RestSharp/src/RestSharp/RestClientExtensions.cs
Lines 325 to 350 in cd4c434
The key here is this line:
RestSharp/src/RestSharp/RestClientExtensions.cs
Line 330 in cd4c434
Racer396 commentedon Mar 16, 2022
CompletionOption = HttpCompletionOption.ResponseHeadersReadYes, that what i need. Thank you so much! :)
kendallb commentedon Mar 31, 2022
I just ran into this also. Wouldn't it make more sense to always set that completion option for DownloadStreamAsync? Otherwise DownloadStreamAsync really is not much point, because it defeats the purpose of actually downloading into a stream?
kendallb commentedon Mar 31, 2022
Also that would make DownloadDataAsync be more efficient, as currently it will end up downloading the data first into a memory stream, which is then streamed into a byte[] array to return the caller. If it was actually streaming the response content, then the byte[] array is built more efficiently (internally it's streamed into a memory stream 16K at a time which is then turned into the byte array). So if we are downloading say a 2MB file, there will be a 2MB memory stream created by the DownloadStreamAsync() function, then another 2MB memory stream that the data gets copied into, which then gets turned into the final byte array.
kendallb commentedon Mar 31, 2022
Here is a PR
#1825