我因
而收到错误<ServiceKnownType(GetType(EmailMessageTemplate))> _
<ServiceKnownType(GetType(SmsMessageTemplate))> _
<OperationContract()> <WebMethod()> _
Public Function GetMessageTemplates(ByVal handlerId As Integer) As Collection(Of MessageTemplate)
Dim result = New Collection(Of MessageTemplate)
result = MessageDataAccess.GetMessageTemplates(handlerId)
Return result
End Function
我是wcf
服务的网络方法(因为我使用:
<%@ServiceHost Language="VB" Service="MyAppITWS.Service1" %>
我将我的应用程序称为服务引用
结果是EmailMessageTemplates
和SmsMessageTemplates
的集合。在这里,我是我的dtacontracts:
[DataContract]
public class EmailMessageTemplate : MessageTemplate
{
[DataMember]
public string EmailSubject { get; set; }
[DataMember]
public EmailFormat EmailFormat { get; set; }
}
[DataContract]
public class EmailMessageTemplate : MessageTemplate
{
[DataMember]
public string EmailSubject { get; set; }
[DataMember]
public EmailFormat EmailFormat { get; set; }
}
[DataContract]
public class SmsMessageTemplate : MessageTemplate
{
}
我该如何解决?
这是一个非常错误:
{System.Runtime.Serialization.SerializationException:第1行位置1535出错。元素 'http://schemas.datacontract.org/2004/07/MyAppUtils.DataModel.Messaging:MessageTemplate' 包含的数据 'http://schemas.datacontract.org/2004/07/MyAppUtils.DataModel.Messaging:EmailMessageTemplate' 数据合同。反序列化器不知道任何映射类型 这个合同。添加与“EmailMessageTemplate”对应的类型 到已知类型列表 - 例如,通过使用 KnownTypeAttribute属性或通过将其添加到已知列表中 传递给DataContractSerializer的类型。 w System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator) reader,String name,String ns,DataContract&amp; dataContract) w System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator) xmlReader,Int32 id,RuntimeTypeHandle declaredTypeHandle,String name,String ns) w ReadArrayOfMessageTemplateFromXml(XmlReaderDelegator,XmlObjectSerializerReadContext,XmlDictionaryString, XmlDictionaryString,CollectionDataContract) w System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator) xmlReader,XmlObjectSerializerReadContext context) w System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract,XmlReaderDelegator reader) w System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator) reader,String name,String ns,DataContract&amp; dataContract) w System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator) xmlReader,Type declaredType,DataContract dataContract,String name, 字符串ns) w System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator) xmlReader,Boolean verifyObjectName) w System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator) reader,Boolean verifyObjectName) w System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader,Boolean verifyObjectName) w System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader) reader,PartInfo part,Boolean isRequest)}
答案 0 :(得分:2)
您是否阅读了错误消息的第一行?
反序列化程序不知道映射到此合同的任何类型。将与“EmailMessageTemplate”对应的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中。
即,将以下属性添加到MessageTemplate
类
[DataContract]
[KnownType(typeof(EmailMessageTemplate)]
public class MessageTemplate
{
}
请参阅this link了解实例
答案 1 :(得分:2)
KnownType通常应用于基本类型 - 即
[DataContract, KnownType(typeof(MessageTemplate)), ...]
(您还可以在DataContractSerializer
构造函数中指定已知类型,而无需属性)
如果框架类不知道所有派生类型,那么在创建序列化程序时需要指定已知类型:
语法是
[KnownType(typeof(SubClass))]
public class BaseClass