How to fetch value from an ExtensionDataObject of a wcf reponse

泄露秘密 提交于 2021-02-11 13:51:30

问题


I have a WCF service which returns ExtensionDataObject during runtime as attached snapshot: Im struck with fetching value for these objects. Could anyone please help here:

Have tried with below code using reflection, which throws Parameter count missing exception

  List<System.Runtime.Serialization.ExtensionDataObject> extData = temp.Select(x => x.ExtensionData).ToList();
             var GetCountry = extData.GetType().GetProperties();

              string Country = string.Empty;
                foreach (var property in GetCountry)
                {
                    string name = property.Name;
                    object value = property.GetValue(extData, null);
                    if (name == "Country")
                        Country = value.ToString();

                }


回答1:


The Extensiondataobject field is generated to control the data contract incompatibility between the server and the client, so it will return a field named extensiondataobject. In other words, your client data contract implements the IExtensionDataObject interface.

    [DataContract(Namespace="abcd")]
    public class Product: IExtensibleDataObject
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Name { get; set; }
        public ExtensionDataObject ExtensionData { get ; set ; }
}

If we capture this request through Fiddle, you can even see all the data directly. In a word, you only need to add the Country property to the Data class of X object. It will be deserialized automatically. This class should be your client-side data contract class, instead of the server-side data class.

Finally, it seems that the value of these fields is null. We should ensure that the server and client data contracts have the same namespace. It cannot be the default value(http://tempuri.org). As I defined above, this namespace attribute should be consistent with the server-side value.
Feel free to let me know if there is anything I can help with.



来源:https://stackoverflow.com/questions/59482688/how-to-fetch-value-from-an-extensiondataobject-of-a-wcf-reponse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!