c# – WCF抽象基类,在服务响应中不包含用于反序列化的抽象类型的复杂集合

我们在应用程序中为所有Response DTO使用基类.该课程有以下签名:
[Serializable]
public abstract class ResponseBase
{
    public bool Successful { get; set; }
    public List<ResponseMessage> Messages { get; set; }

    //...Other code...
}

Messages集合可以是以下任何类型:

[Serializable]
[XmlInclude(typeof(DebugMessage))]
[XmlInclude(typeof(InfoMessage))]
[XmlInclude(typeof(ValidationMessage))]
[XmlInclude(typeof(WarnMessage))]
[XmlInclude(typeof(RecoverableFaultMessage))]
[XmlInclude(typeof(FatalFaultMessage))]
public abstract class ResponseMessage
{
    //..Other code...
}

具体版本:

[Serializable]
public class DebugMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.Debug; } }
}
[Serializable]
public class InfoMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.Info; } }
}
[Serializable]
public class ValidationMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.Validation; } }
}
[Serializable]
public class WarnMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.Warn; } }
}
[Serializable]
public class RecoverableFaultMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.RecoverableFault; } }
}
[Serializable]
public class FatalFaultMessage : ResponseMessage
{
    public override MessageType MessageType { get { return MessageType.FatalFault; } }
}

所有DTO Response对象都继承自ResponseBase,但即使在WCF契约中使用以下ServiceKnownTypes也是如此

[ServiceKnownType(typeof(ResponseBase))]
[ServiceKnownType(typeof(ResponseMessage))]
[ServiceKnownType(typeof(List<ResponseMessage>))]
[ServiceKnownType(typeof(DebugMessage))]
[ServiceKnownType(typeof(InfoMessage))]
[ServiceKnownType(typeof(ValidationMessage))]
[ServiceKnownType(typeof(WarnMessage))]
[ServiceKnownType(typeof(RecoverableFaultMessage))]
[ServiceKnownType(typeof(FatalFaultMessage))]
[ServiceKnownType(typeof(List<DebugMessage>))]
[ServiceKnownType(typeof(List<InfoMessage>))]
[ServiceKnownType(typeof(List<ValidationMessage>))]
[ServiceKnownType(typeof(List<WarnMessage>))]
[ServiceKnownType(typeof(List<RecoverableFaultMessage>))]
[ServiceKnownType(typeof(List<FatalFaultMessage>))]

当我们将Message加载到ResponseBase Messages集合时,会抛出以下异常:

Error in line 1 position 906. Element
‘http://schemas.datacontract.org/2004/07/CX.Framework.Common.BaseTypes:ResponseMessage’
contains data from a type that maps to the name
‘http://schemas.datacontract.org/2004/07/CX.Framework.Common.BaseTypes:WarnMessage’.
The deserializer has no knowledge of any type that maps to this name.
Consider using a DataContractResolver or add the type corresponding to
‘WarnMessage’ to the list of known types – for example,by using the
KnownTypeAttribute attribute or by adding it to the list of known
types passed to DataContractSerializer.

我们已经在派生类型上完成了从ServiceKnownType到XMLInclude的所有操作.我对如何解决这个问题感到有点失落,并感谢任何人都能提供的帮助.

解决方法

一些东西:

1)[XmlInclude]对DataContractSerializer没有影响,它只由XmlSerializer使用.

2)正如评论者“flem”建议的那样,我会直接在ResponseMessage上使用[KnownType]而不是服务上的[ServiceKnownType].

3)我不记得DataContractSerializer是否在[Serializable]类型上查找[KnownType].至少目前为了测试目的,如果上面的#2没有帮助,请尝试改为使用[DataContract]类型(并使用[DataMember]对数据成员进行属性化).

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString(&quot;x2&quot;));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable&lt;Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include &quot;WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...