首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wcf客户端在反序列化过程中忽略派生类型。

wcf客户端在反序列化过程中忽略派生类型。
EN

Stack Overflow用户
提问于 2013-09-04 16:13:16
回答 1查看 420关注 0票数 3

我使用标准的wsdl (位于这里)与服务(OpenXDS)通信。我从它创建了一个服务引用,它生成了一个非常大的Reference.cs文件。文件中有如下类型的层次结构:

代码语言:javascript
复制
public partial class ExtrinsicObjectType : RegistryObjectType

。。。

代码语言:javascript
复制
[System.Xml.Serialization.XmlIncludeAttribute(typeof(ExtrinsicObjectType))]
public partial class RegistryObjectType : IdentifiableType

。。。

代码语言:javascript
复制
[System.Xml.Serialization.XmlIncludeAttribute(typeof(RegistryObjectType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(ExtrinsicObjectType))]
public partial class IdentifiableType : object

这三种类型都具有相同的XmlType:

代码语言:javascript
复制
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0")]

IdentifiableType对象的响应类型中有一个集合:

代码语言:javascript
复制
[System.Xml.Serialization.XmlArrayAttribute(Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0", Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("Identifiable", IsNullable=false)]
public IdentifiableType[] RegistryObjectList { 

当服务实际响应时,它提供了一个ExtrinsicObject元素的集合:

代码语言:javascript
复制
<rim:RegistryObjectList xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
  <ns1:ExtrinsicObject xmlns:ns1="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" ...
  <ns1:ExtrinsicObject xmlns:ns1="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" ...
  <ns1:ExtrinsicObject xmlns:ns1="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" ...
  <ns1:ExtrinsicObject xmlns:ns1="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" ...
  <ns1:ExtrinsicObject xmlns:ns1="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" ...
</rim:RegistryObjectList>

我在跟踪日志中看到了这些元素,我可以在SoapUI中得到相同的答案。但是当我从客户机代理获得反序列化响应时,RegistryObjectList是空的。它完全忽略了ExtrinsicObject元素。

我无法更改服务器,客户端由VS2012生成。看上去这应该能起作用,但我错过了一些环境什么的。

以下是我到目前为止的理论:

  • 服务引用上有一些设置,如果我检查它并更新代码,一切都会正常工作。
  • 我使用的wsdl与他们同意的wsdl不同。
  • 我必须弄清楚如何手动反序列化响应。

任何帮助都是非常感谢的。我试图包含我认为相关的内容,但是由于wsdl、xsd和Reference.cs都相当大,所以进行了大量的编辑。

EN

回答 1

Stack Overflow用户

发布于 2016-12-14 22:10:37

通过将以下XmlAttributeOverrides传递给序列化程序,我就完成了这个任务:

代码语言:javascript
复制
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(SubmitObjectsRequest), "RegistryObjectList", new XmlAttributes
{
    XmlArray = new XmlArrayAttribute()
    {
        Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0",
        Order = 0
    },
    XmlArrayItems =
    {
        new XmlArrayItemAttribute("ExtrinsicObject", typeof(ExtrinsicObjectType)) { IsNullable = false },
        new XmlArrayItemAttribute("RegistryPackage", typeof(RegistryPackageType)) { IsNullable = false }
    }
});

据我所知,正如您所说的,Reference.cs给出了以下内容:

代码语言:javascript
复制
[System.Xml.Serialization.XmlArrayAttribute(Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0", Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("Identifiable", IsNullable=false)]
public IdentifiableType[] RegistryObjectList {

但是,由于在XML中,RegistryObjectList元素没有Identifiable子节点,而是有ExtrinsicObjectRegistryPackage子节点,所以我真正希望它看起来如下所示:

代码语言:javascript
复制
[System.Xml.Serialization.XmlArrayAttribute(Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0", Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("ExtrinsicObject", typeof(ExtrinsicObjectType), IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("RegistryPackage", typeof(RegistryPackageType), IsNullable=false)]
public IdentifiableType[] RegistryObjectList {

并且,包含重写将使序列化程序忽略原始属性,并将RegistryObjectList视为已用后者修饰过的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18618920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档