首页 > 解决方案 > .NET 4.8 - WCF 复杂架构 - 使用 XmlSerializer 将派生类型作为基础呈现

问题描述

由于继承,我在使用具有复杂架构的 SOAP 服务时遇到重大问题

我有一个基本类型ProcessMessage

[System.Xml.Serialization.XmlIncludeAttribute(typeof(ProcessCustomerMessage))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.8.3928.0")]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract class ProcessMessage
{

}

然后我有多个派生类型,例如ProcessCustomerMessage ProcessRepairMessage继承基类。

问题是我需要将派生类型作为基础,以便服务接受它

我已经遵循了我可以添加的文档XmlInclude以及XmlType在需要的地方...

/// <remarks/>
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlType(TypeName = "ProcessMessage", AnonymousType = true, Namespace = "http://www.mynamespace.org")]
public class ProcessCustomerMessage: ProcessMessage {

  private ProcessCustomerInformationPayload payloadField;

  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
  public ProcessCustomerInformationPayload payload {
    get {
      return this.payloadField;
    }
    set {
      this.payloadField = value;
    }
  }
}

上面的代码将正确序列化,但服务拒绝它并出现以下错误

Unknown type {tran:ProcessMessage} specified in xsi:type attribute

这是因为xsi:type="tran:ProcessMessage"在 Xml

<tran:ProcessMessage xsi:type="tran:ProcessMessage" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

我看不到如何xsi:type从 Xml中删除它

我使用的是 XmlSerializer 而不是 DCS,因为架构非常复杂,不受 DCS 支持,我也无法控制架构作为其第 3 方服务。

标签: c#.netxmlwcf

解决方案


推荐阅读