James Becwar
James Becwar

Reputation: 1206

HttpClient response ReadAsAsync() doesn't fully deserialize object

I'm trying to consume a web service with the Web API client library. My problem is that the ReadAsAsync doesn't seem to want to fully deserailize the returned object when the submitting function uses a POST method.

If I get the response as a string and manually deserailize it works. (I get a apmsgMessage with all the fields populated)

HttpClient client = GetClient();
var response = client.PostAsJsonAsync("api/robot/Preview", ad).Result;
var msg = response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<apmsgMessage>(msg.Result);

I originally tried the code below which returns an apmsgMessage Object, but all the fields are null.

HttpClient client = GetClient();
var response = client.PostAsJsonAsync("api/robot/Preview", ad).Result;
var msg = response.Content.ReadAsAsync<apmsgMessage>().Result;
return msg;

My question is why dosn't my orginal (the PostAsJsonAsync) return a apmsgMessage fully populated. Am I doing somethign wrong with the ReadAsAsync?

Upvotes: 5

Views: 5576

Answers (1)

Joe Enos
Joe Enos

Reputation: 40413

I just had the same issue, and in my case I solved it by removing the [Serializable] attribute from the class.

I don't know why this attribute conflicts with the deserialization process, but as soon as I took that out, the ReadAsAsync method worked as expected.

Upvotes: 5

Related Questions