ExtensionDataObject未标记为可序列化

时间:2015-08-17 17:57:01

标签: c# wcf serialization datacontractserializer binaryformatter

喂!

我在序列化会话状态时遇到问题。我们有2个组件,我们的WCF和Web。根据我们的AdministrationPartial.cs和Administration.svc,我们生成" Administration.cs"我们的Web项目的代码包含以下.bat文件:

svcutil.exe http://wcf_url.local/Administration.svc?wsdl /r:"{Path}\{Namespace}.dll" /d:"{Path}\{Namespace}\Code"

我从上述语句中删除了个人数据,并将其替换为{path}和{namespace}。 Administration.cs将在代码图中。

在Partial中我们有:

[Serializable]
public partial class MyObject
{
    <Some code>
}

它生成了以下代码:

namespace {mynamespace}
{
          using System.Runtime.Serialization

          [System.Diagnostics.DebuggerStepThroughAttribute()]
          [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
          [System.Runtime.Serialization.DataContractAttribute(Name="MyObject", Namespace="http://schemas.datacontract.org/2004/07/{namespace}")]
          public partial class MyObject : object, System.Runtime.Serialization.IExtensibleDataObject
          {
                    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
                    ...... generated code

我做错了什么?

编辑:实际错误为:Type 'System.Runtime.Serialization.ExtensionDataObject' in Assembly 'System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

2 个答案:

答案 0 :(得分:3)

您的问题似乎是,如何为[Serializable]创建BinaryFormatter的课程,并为IExtensibleDataObject实施DataContractSerializer

答案是,这不是开箱即用的,因为您已经注意到,ExtensionDataObject未标记为可序列化。然而,它可以通过一些额外的编码来完成。无论出于何种原因,Microsoft选择使ExtensionDataObject成为一个完全不透明的指针,没有公共属性或其他方式来访问其中的数据。除了 可以通过使用DataContractSerializer重新序列化为XML来访问内部数据。这表明了一种使MyObject类可序列化的方法:将扩展数据存储在实现ISerializable的代理容器字段中,并在内部将扩展数据序列化和反序列化为XML。

以下代理包装器完成此任务:

[Serializable]
public struct ExtensionDataObjectSerializationProxy : ISerializable
{
    public static implicit operator ExtensionDataObjectSerializationProxy(ExtensionDataObject data) { return new ExtensionDataObjectSerializationProxy(data); }

    public static implicit operator ExtensionDataObject(ExtensionDataObjectSerializationProxy proxy) { return proxy.ExtensionData; }

    private readonly System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    public ExtensionDataObject ExtensionData { get { return extensionDataField; } }

    [DataContract(Name = "ExtensionData", Namespace = "")]
    sealed class ExtensionDataObjectSerializationContractProxy : IExtensibleDataObject
    {
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        #region IExtensibleDataObject Members

        public ExtensionDataObject ExtensionData
        {
            get
            {
                return extensionDataField;
            }
            set
            {
                extensionDataField = value;
            }
        }

        #endregion
    }

    public ExtensionDataObjectSerializationProxy(ExtensionDataObject extensionData)
    {
        this.extensionDataField = extensionData;
    }

    public ExtensionDataObjectSerializationProxy(SerializationInfo info, StreamingContext context)
    {
        var xml = (string)info.GetValue("ExtensionData", typeof(string));
        if (!string.IsNullOrEmpty(xml))
        {
            var wrapper = DataContractSerializerHelper.LoadFromXML<ExtensionDataObjectSerializationContractProxy>(xml);
            extensionDataField = (wrapper == null ? null : wrapper.ExtensionData);
        }
        else
        {
            extensionDataField = null;
        }
    }

    #region ISerializable Members

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (ExtensionData != null)
        {
            var xml = DataContractSerializerHelper.GetXml(new ExtensionDataObjectSerializationContractProxy { ExtensionData = this.ExtensionData });
            info.AddValue("ExtensionData", xml);
        }
        else
        {
            info.AddValue("ExtensionData", (string)null);
        }
    }

    #endregion
}

public static class DataContractSerializerHelper
{
    public static string GetXml<T>(T obj, DataContractSerializer serializer = null)
    {
        using (var textWriter = new StringWriter())
        {
            using (var xmlWriter = XmlWriter.Create(textWriter))
            {
                (serializer ?? new DataContractSerializer(typeof(T))).WriteObject(xmlWriter, obj);
            }
            return textWriter.ToString();
        }
    }

    public static T LoadFromXML<T>(string xml, DataContractSerializer serializer = null)
    {
        using (var textReader = new StringReader(xml ?? ""))
        using (var xmlReader = XmlReader.Create(textReader))
        {
            return (T)(serializer ?? new DataContractSerializer(typeof(T))).ReadObject(xmlReader);
        }
    }
}

然后按如下方式手动修改MyObject课程:

public partial class MyObject : object, System.Runtime.Serialization.IExtensibleDataObject
{
    private ExtensionDataObjectSerializationProxy extensionDataField; // Use the proxy not ExtensionDataObject directly

    public ExtensionDataObject ExtensionData
    {
        get
        {
            return extensionDataField;
        }
        set
        {
            extensionDataField = value;
        }
    }
}

答案 1 :(得分:1)

更简单的回答请参阅:http://blogs.msdn.com/b/mohamedg/archive/2010/02/15/extensiondataobject-is-not-marked-as-serializable.aspx

只需将私有ExtensionDataObject标记为非序列化:

[NonSerialized]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;