Click here to Skip to main content
15,885,988 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code below which calls a MicroService called "RydoService" which returns a HttpResponse containing the data stored in "response".

First Question : Is the content in the response serialized ?

If yes, then why do we need to use ReadAsByteArrayAsync() on the response content ?
and then later we are deserialising the content. That I get it.

My Question is that Isnt RydoService already sent the serialised content here over the network ?

public async Task<(bool IsSuccess, IEnumerable<Order> Orders, string ErrorMessage)> GetOrdersAsync(int customerId)
      {
          try
          {
              var client = httpClientFactory.CreateClient("RydoOrderService");
              var response = await client.GetAsync($"api/orders/{customerId}");
              if(response.IsSuccessStatusCode)
              {
                  var content = await response.Content.ReadAsByteArrayAsync();
                  var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
                  var result = JsonSerializer.Deserialize<IEnumerable<Order>>(content, options);
                  return (true, result, null);
              }

              return (false, null, response.ReasonPhrase);
          }
          catch(Exception ex)
          {
              logger?.LogInformation(ex.ToString());
              return (false, null, ex.Message);
          }
      }


What I have tried:

Googling. No Luck. So Help :)
Waiting..........................................
Posted
Updated 21-Jun-21 4:17am
Comments
Richard MacCutchan 19-Jun-21 7:53am    
Ask the people who provide the service.
vaibhav1800 19-Jun-21 8:06am    
I have only developed that microservice and I am consuming it. Its not a professional app. I just want to understand the point of using ReadAsByteArrayAsync() here. I know at what point the content is being deserialised here but I am confused at which point the content got serialised. I have not used any serialization logic n RydoService Microservice when sending data to this API where I am consuming this RydoService
Richard MacCutchan 19-Jun-21 10:00am    
If you do not understand the code that you have written, how do you expect anyone else to?
vaibhav1800 19-Jun-21 12:59pm    
Well only the piece of code which I mentioned. I was going through a Microservices tutorial in Udemy. :)
Richard MacCutchan 20-Jun-21 3:40am    
Then you should be talking to the person who wrote the tutorial.

1 solution

Based on the code you have provided, the response body is encoded as JSON[^].

You use ReadAsBytesAsync to load the response into an array of bytes. You could alternatively use ReadAsStringAsync to get a string, or ReadAsStreamAsync to get a Stream. In this case, you're getting an array of bytes because that's what your JsonSerializer.Deserialize method wants.

As an alternative:
In .NET Framework and .NET Core, you could add a reference to the Microsoft.AspNet.WebApi.Client NuGet package, and use ReadAsAsync<IEnumerable<Order>> to automatically deserialize the JSON for you.

In .NET 5, this seems to have been replaced with the ReadFromJsonAsync<IEnumerable<Order>> extension method, defined in System.Net.Http.Json.HttpContentJsonExtensions.
 
Share this answer
 
Comments
vaibhav1800 22-Jun-21 6:23am    
I got my answer. Thank You. Crisp and Clear.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900