我正在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.
我已互换Entity
和Annotation
,但无济于事。
哪种方法更好,最接近我需要做的?请让我知道您的建议或答案。我已经被困在这里好几天了。非常感谢!
答案 0 :(得分:0)
我不相信旧的“早期绑定类型”和Entity
与新的网络API兼容。您可以创建自己的POCO和/或应该有可以使用webapi的元数据端点为您生成类的生成器。