简体   繁体   English

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

[英]InvalidDataContractException (“Type cannot be serialized”) error using WCF DataContract

I need to pass System.Net.NetworkInformation.PhysicalAddress (.NET Framework built-in type) in my data contract. 我需要在我的数据协定中传递System.Net.NetworkInformation.PhysicalAddress (.NET Framework内置类型)。 Since this type cannot be serialized by itself, I derived my helper class from it: 由于这种类型不能自行序列化,我从中派生了我的助手类:

[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); } }
}

It serializes to XML pefectly; 它完美地序列化为XML; however, when I try to pass it into service call, I'm getting System.Runtime.Serialization.InvalidDataContractException: 但是,当我尝试将其传递给服务调用时,我得到System.Runtime.Serialization.InvalidDataContractException:

Type 'System.Net.NetworkInformation.PhysicalAddress' cannot be serialized. 类型'System.Net.NetworkInformation.PhysicalAddress'无法序列化。 Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. 请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员。 If the type is a collection, consider marking it with the CollectionDataContractAttribute. 如果类型是集合,请考虑使用CollectionDataContractAttribute对其进行标记。 See the Microsoft .NET Framework documentation for other supported types. 有关其他受支持的类型,请参阅Microsoft .NET Framework文档。

Stack trace: 堆栈跟踪:

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)

As per the error message, it is unhappy about PhysicalAddress , the base-class. 根据错误消息,它对基础类PhysicalAddress不满意。 So either: 所以要么:

  • don't use the base-class - use a simple DTO that doesn't need it, or: 不要使用基类 - 使用不需要它的简单DTO,或者:
  • mark the base-class for serialization 标记基类以进行序列化

Specifically, something like: 具体来说,类似于:

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

However! 然而! From the name PhysicalAddressSerializable , it sounds like you don't want to have the base-class as serializable. 从名称PhysicalAddressSerializable ,听起来你不希望将基类作为可序列化。 You could try using: 您可以尝试使用:

[XmlSerializerFormat]
public class PhysicalAddressSerializable : PhysicalAddress {...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM