从ReadAsStreamAsync或ReadAsStringAsync反序列化为CRM实体

时间:2016-07-19 01:39:40

标签: c# deserialization dynamics-crm httpresponse json-deserialization

我正在HttpResponseMessage response = Helper.SendRequest(...)调用Web API,我需要将response反序列化为list CRM实体Annotation

我们尝试过的一件事是使用ReasAsStreamAsync

List<Annotation> _listAnnotation = new List<Annotation>();
MemoryStream stream1 = (MemoryStream)response.Content.ReadAsStreamAsync().Result;
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Annotation>));

ser.WriteObject(stream1, _listAnnotation);
stream1.Position = 0;
List<Annotation> lst = (List<Annotation>)ser.ReadObject(stream1);

但它会引发异常Stream does not support writing.

我们也尝试了ReadAsStringAsync

var jsonString = response.Content.ReadAsStringAsync();
jsonString.Wait();
List<Entity> model = (List<Entity>)JsonConvert.DeserializeObject<List<Entity>>(jsonString.Result);

jsonString的值为

[{"Id":"00000000-0000-0000-0000-000000000000","LogicalName":"annotation","Attributes":[{"Key":"filename","Value":"Microsoft.SharePoint.Client.FileInformation"},{"Key":"documentbody","Value":"JVBERi0xLjUKJfv8/f4KNCAwIG9iago8PAovVGFicyAvUwovU3RydWN0UGFyZW50cyAwCi9QYXJlbnQgMyAwIFIKL1Jlc291cmNlcyA2IDAgUgo......g=="},{"Key":"mimetype","Value":"text/plain"}],"EntityState":null,"FormattedValues":[],"RelatedEntities":[]}]

但它说Cannot create and populate list type Microsoft.Xrm.Sdk.AttributeCollection.

我已互换EntityAnnotation,但无济于事。

哪种方法更好,最接近我需要做的?请让我知道您的建议或答案。我已经被困在这里好几天了。非常感谢!

1 个答案:

答案 0 :(得分:0)

我不相信旧的“早期绑定类型”和Entity与新的网络API兼容。您可以创建自己的POCO和/或应该有可以使用webapi的元数据端点为您生成类的生成器。