-1

I retrieve a huge JSON data from the server
The problem is that, the serialization process have performance cost, in order to minimize this cost I need to retrieve this data as a stream first and then, with the help of Newtonsoft's Json.net library serialize the data as a stream to the required type.
Now I'm doing this:

using (var stream = await httpClient.GetStreamAsync(requestUrl))
{
    using(var streamReader = new StreamReader(stream))
    {
        using(var jsonReader = new JsonTextReader(streamReader))
        {
            var items = jsonSerializer.Deserialize<TEntity[]>(jsonReader);
            //Some logic
        }
    }
}

The above code is good, but I need to get the HttpStatusCode from the response object, but I cannot get the response object while working with streams. I searched for a solution and found this question.
Now, my question is: Is there any difference between the two approaches?
Calling GetAsync to get the response object and then calling response.Content.ReadAsStreamAsync will it have performance overhead?
I mean how does HttpClient do it?
Does it load the data into the memory and then when I call response.Content.ReadAsStreamAsync I read the data from the memory as stream?
Or, when I call response.Content.ReadAsStreamAsync I'm getting the data as stream directly from the server?

Rickless
  • 1,377
  • 3
  • 17
  • 36
  • 1
    `Now, my question is: Is there any difference between the two approaches?` Did you try the two of them? Did they act the same? – mjwills Jun 26 '18 at 12:31
  • No, I haven't. I was looking for a way to use `stream` for the performance and getting the `HttpStatusCode` at the same time. In the other question, the answer doesn't explain if they do the same work. I was wondering if someone have tried both ways. – Rickless Jun 26 '18 at 12:35

1 Answers1

2

HttpCompletionOption.ResponseHeadersRead is your friend.

The operation should complete as soon as a response is available and headers are read. The content is not read yet.

You can use it in some overloads of HttpClient methods:

    using (HttpResponseMessage response = await client.GetAsync(url, 
                                     HttpCompletionOption.ResponseHeadersRead))
    using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
    {
        //...

HttpCompletionOption controls whether or not the response is handed back to the caller after it has completely downloaded (ResponseContentRead) or immediately after the headers are received (ResponseHeadersRead), which allows for streaming and conservation of memory resources.

spender
  • 117,338
  • 33
  • 229
  • 351