【问题标题】:wcf client ignores derived types during deserializationwcf 客户端在反序列化期间忽略派生类型
【发布时间】:2013-09-04 16:13:16
【问题描述】:

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

public partial class ExtrinsicObjectType : RegistryObjectType

。 . .

[System.Xml.Serialization.XmlIncludeAttribute(typeof(ExtrinsicObjectType))]
public partial class RegistryObjectType : IdentifiableType

。 . .

[System.Xml.Serialization.XmlIncludeAttribute(typeof(RegistryObjectType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(ExtrinsicObjectType))]
public partial class IdentifiableType : object

所有三种类型都有相同的 XmlType:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0")]

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

[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 元素的集合:

<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 都相当大,因此进行了很多编辑。

【问题讨论】:

标签: c# wcf soap wsdl deserialization


【解决方案1】:

我刚刚通过将以下 XmlAttributeOverrides 传递给序列化程序来完成这项工作:

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,正如你所说,给出了这个:

[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 子节点,我真正想要的是它看起来像这样:

[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 视为已用后者修饰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2017-03-22
    相关资源
    最近更新 更多