HttpClient的响应ReadAsAsync()不完全反序列化对象 [英] HttpClient response ReadAsAsync() doesn't fully deserialize object

查看:1733
本文介绍了HttpClient的响应ReadAsAsync()不完全反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图消耗与Web API客户端库的Web服务。我的问题是,ReadAsAsync似乎并不想完全deserailize返回的对象的时候,提交功能使用POST方法。

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.

如果我得到的回应为一个字符串,手动deserailize它的作品。 (我得到一个apmsgMessage与填充的所有字段)

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);

我最初试图在code,低于该返回apmsgMessage对象,但所有字段为空。

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;

我的问题是,为什么不到风度我的原单(在PostAsJsonAsync)返回一个apmsgMessage完全填充。我做somethign错误的ReadAsAsync?

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

推荐答案

我刚刚有同样的问题,在我的情况下,我通过删除解决它的 [Serializable接口] 从类属性。

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

我不知道为什么与反序列化进程这个属性的冲突,但只要我带了这一点,在 ReadAsAsync 方法和预期一样。

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.

这篇关于HttpClient的响应ReadAsAsync()不完全反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆