如何从WCF响应的ExtensionDataObject中获取值 [英] How to fetch value from an ExtensionDataObject of a wcf reponse

查看:97
本文介绍了如何从WCF响应的ExtensionDataObject中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务,该服务在运行时作为附加的快照返回ExtensionDataObject:我为这些对象的价值而吃惊.任何人都可以在这里提供帮助:

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

                }

推荐答案

生成了 Extensiondataobject 字段以控制服务器和客户端之间的数据协定不兼容,因此它将返回一个名为 extensiondataobject .换句话说,您的客户数据合同实现了 IExtensionDataObject 接口.

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

如果我们通过Fiddle捕获此请求,您甚至可以直接查看所有数据.简而言之,您只需将 Country 属性添加到X对象的Data类中.它将自动反序列化.该类应该是您的客户端数据协定类,而不是服务器端数据类.

最后,这些字段的值似乎为空.我们应该确保服务器和客户端数据协定具有相同的名称空间.它不能是默认值( http://tempuri.org ).正如我在上面定义的那样,此名称空间属性应与服务器端值一致.
请随时告诉我是否有什么我可以帮助的.

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.

这篇关于如何从WCF响应的ExtensionDataObject中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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