强类型数据集的序列化

时间:2012-02-24 22:24:33

标签: asp.net .net vb.net appfabric

有没有人知道如何制作强类型数据集(嘿不是我的想法,使用它们)可序列化,以便我可以使用AppFabric Server作为会话状态提供程序存储它们?我一直在使用InProc Session状态一段时间,并且没有遇到任何问题。一旦我将会话状态移到AppFabric服务器上,我就会遇到强类型数据集的一些问题。我一直收到一个错误,告诉我我的数据集不可序列化。我已经检查过它们上面有Serializable属性,它们只包含可序列化的数据类型(intstring等等。)任何想法?

编辑:这是堆栈跟踪:

System.Runtime.Serialization.SerializationException: The constructor to deserialize an object of type ''DataSetClasses.MyStronglyTypedDataset'' was not found. ---> System.Runtime.Serialization.SerializationException: The constructor to deserialize an object of type ''DataSetClasses.MyStronglyTypedDataset'' was not found. at System.Runtime.Serialization.ObjectManager.GetConstructor(RuntimeType t, RuntimeType[] ctorParams) at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) --- End of inner exception stack trace --- at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder) at System.Runtime.Serialization.ObjectManager.DoFixups() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Web.Util.AltSerialization.ReadValueFromStream(BinaryReader reader) at System.Web.SessionState.SessionStateItemCollection.ReadValueFromStreamWithAssert() at System.Web.SessionState.SessionStateItemCollection.DeserializeItem(String name, Boolean check) at System.Web.SessionState.SessionStateItemCollection.get_Item(String name) at System.Web.SessionState.HttpSessionStateContainer.get_Item(String name) at System.Web.SessionState.HttpSessionState.get_Item(String name) at appt.T2SharedLibrarySetup.btnSave_Click(Object sender, EventArgs e) in D:\Application\ProblemFile.aspx.vb:line 331 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent

2 个答案:

答案 0 :(得分:1)

首先尝试将对象序列化/反序列化为字节数组,然后再尝试将其放入会话(从而进入AppFabric)。这是一个例子。

        private static BinaryFormatter formatter = null;

    private static Byte[] Serialize(object entity)
    {
        Logger.LogDebug(Category.Cache, "Serializing");
        Byte[] bytes;
        if (formatter == null) formatter = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream())
        {
            formatter.Serialize(stream, entity);
            bytes = stream.ToArray();
        }
        return bytes;
    }

    private static object DeSerialize(Byte[] bytes)
    {
        #region Sanitation
        if (bytes == null) { throw new System.ArgumentNullException("bytes"); }
        #endregion
        Logger.LogDebug(Category.Cache, "DeSerializing");
        object obj;
        if (formatter == null) formatter = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream(bytes))
        {
            obj = formatter.Deserialize(stream);
        }
        return obj;
    }

从反序列化程序返回对象后,尝试将其强制转换为类型化数据集类型。

答案 1 :(得分:0)

您可以使用DataSet.ReadXML()或WriteXML()函数来实现所需的功能。