当前位置: 首页 > 知识库问答 >
问题:

使用WCF DataContract时出现InvalidDataContractException(“无法序列化类型”)错误

凤高澹
2023-03-14

我需要通过系统。网网络信息。数据协定中的PhysicalAddress(.NET Framework内置类型)。由于此类型本身无法序列化,因此我从中派生了助手类:

[DataContract]
public class PhysicalAddressSerializable : PhysicalAddress
{
    public PhysicalAddressSerializable()
        : base(new byte[] {0,0,0,0,0,0})
    { }
    public PhysicalAddressSerializable(byte[] address) 
        : base(address)
    { }

    [DataMember]
    public string AddressString { get { return ToString(); } set { Parse(value); } }
}

它严重地序列化为XML;然而,当我试图将其传递给服务调用时,我得到了System.运行时。连载。无效数据合同异常:

“类型”系统。网网络信息。无法序列化PhysicalAddress“”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。如果类型是集合,请考虑用CopyDATAcNoTractAttor标记它。见微软。其他受支持类型的NET Framework文档。

堆栈跟踪:

Server stack trace: 
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.DataContract.GetDataContract(Int32 id, RuntimeTypeHandle typeHandle, SerializationMode mode)
   at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.DataContract.GetDataContract(Int32 id, RuntimeTypeHandle typeHandle, SerializationMode mode)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteUnitInfoToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, Object[] parameters)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
   at System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at System.ServiceModel.Channels.FramingDuplexSessionChannel.EncodeMessage(Message message)
   at System.ServiceModel.Channels.FramingDuplexSessionChannel.OnSendCore(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.OnSend(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.OutputChannel.Send(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

共有1个答案

法和安
2023-03-14

根据错误消息,它对基类物理地址不满意。所以要么:

  • 不要使用基类-使用不需要的简单DTO,或者:

具体来说,类似于:

[DataContract, KnownType(PhysicalAddressSerializable)]
public class PhysicalAddress {
    [DataMember(...)]
    public SomeType Foo {get;set;} // whatever
}

然而从名称PhysicalAddressSerializable,听起来您不希望基类是可序列化的。您可以尝试使用:

[XmlSerializerFormat]
public class PhysicalAddressSerializable : PhysicalAddress {...}
 类似资料:
  • 这是我的视图模型: 我正在用数据加载视图模型,并将其传递给视图并序列化它: 但是我得到了这个错误 “类型”系统。网状物无法序列化HttpPostedFileWrapper“”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。如果类型是集合,请考虑用CopyDATAcNoTractAttor标记它。见微软。其他受支持类型的NE

  • 我试图使用混合序列化集合,但Jackson不会保存类型信息。这是一个基本测试,说明会发生什么: 如何在第一个输出上实现输出?

  • 我有一个带有Object类型属性的基类型(一段遗留代码,许多项目都使用这个基类型)。后来添加了基类型的泛型版本,将属性公开为泛型类型。 使用ServiceStack.Text序列化和反序列化泛型类型将设置基类(type object)上的属性,而不是派生类上更特定的类型。 重现错误的简单控制台应用程序如下所示: 感谢任何帮助。 基于这个答案,我通过使和从一个新的抽象基类继承来解决这个问题,如下所示

  • 我有一门课是这样的: 但是当我试图序列化它时,我收到一个错误,上面写着“试图序列化java.lang.class:java.lang.字符串。忘记注册一个类型适配器了吗?”。所以我创建了这个适配器: } 并登记如下: 但我还是犯了同样的错误<我做错了什么 适配器的实现看起来正常吗?

  • 我在序列化Jackson中的泛型对象时遇到问题。我已经找到了一个解决方法,但我仍然不知道为什么我原来的解决方案不起作用。以下是我的设置: 以下是我当前用于序列化方法的代码: 以下是应该起作用但不起作用的: 对于第二种情况,我得到了以下例外: 这是什么原因?从ClassB中的类型规范可以清楚地看出,列表中只能使用ParamClass,不能使用BaseParamClass。我错过什么了吗?

  • 我有一行代码: 它接受一个变量,其类型应为float。当我使用type()测试变量类型时,它返回: 但是我一直收到一个错误,上面写着“/: str和int不支持的操作数类型”。