west
west

Reputation: 384

HttpResponseMessage.Content.ReadAsStreamAsync() vs HttpResponseMessage.Content.ReadAsStringAsync()

var request = new HttpRequestMessage(HttpMethod.Get, $"api/Items");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

using (var response = await _httpClient.SendAsync(request))
{
   response.EnsureSuccessStatusCode();
   var stream = await response.Content.ReadAsStreamAsync();        

     using (var streamReader = new StreamReader(stream))
     {
       using (var jsonTextReader = new JsonTextReader(streamReader))
       {
         var jsonSerializer = new JsonSerializer();
         var data =  jsonSerializer.Deserialize<Item>(jsonTextReader);
       }
     }
}

...

var request = new HttpRequestMessage(HttpMethod.Get, "api/Items");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.SendAsync(request);

response.EnsureSuccessStatusCode();

var content = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<List<Item>>(content);

I've run this two examples and I'm curious what is the difference between them that always getting same result, ReadAsStreamAsync is much faster then ReadAsStringAsync.

Upvotes: 17

Views: 23079

Answers (3)

Random Person
Random Person

Reputation: 49

This answer is not directly related to the question but rather to the code I see.

As I can see you do the deserialization in a synchronous manner, instead you should do something like this:

using HttpClient httpClient = new();

using HttpResponseMessage response = await httpClient.GetAsync(
    "...",
    HttpCompletionOption.ResponseHeadersRead
).ConfigureAwait(false);

response.EnsureSuccessStatusCode();

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

await foreach (WeatherForecast weatherForecast in weatherForecasts)
{
    ...
}

Please be aware that: DefaultBufferSize while passing JsonSerializerOptions to the DeserializeAsyncEnumerable method. This is very important if one wants to achieve streaming behavior. Internally, DeserializeAsyncEnumerable will read from the stream until the buffer is full or the stream has ended. If the buffer size is large (and the default is 16KB) there will be a significant delay in asynchronous iteration (in fact you can see irregularity in the above output resulting from exactly that).

Creds - https://www.tpeczek.com/2021/07/aspnet-core-6-and-iasyncenumerable.html

Upvotes: 0

DarkFinger
DarkFinger

Reputation: 131

Under the hood, HttpClient stores content in MemoryStream. So, basically calling ReadAsStreamAsync just returns a reference to the stream. In the case of calling ReadAsStringAsync, ToArray method is called on memory stream so an additional copy of data is created.

Upvotes: 11

Vlad
Vlad

Reputation: 164

You can check description for ReadAsStreamAsync and for ReadAsStringAsync

In few words - you can send to request content not only string. And ReadAsStreamAsync is only way for you here. If your response content is string - you can use both. But Stream is faster in Any Time.

This gives good explanation about memory allocation and perfomanse in these cases.

Upvotes: 4

Related Questions