2012-02-24 169 views
0

有沒有人知道一種方法來創建一個強類型數據集(嘿,不是我的想法使用它們)可序列化,以便我可以將它們作爲會話狀態提供程序使用AppFabric Server存儲?我一直在使用InProc會話狀態一段時間,並沒有任何問題。只要我將會話狀態移出到AppFabric Server,我就會遇到強類型數據集的一些問題。我不斷收到一個錯誤,告訴我我的數據集不可序列化。我已經檢查過,他們有Serializable屬性,它們只包含可序列化的數據類型(int,string等)任何thougnts?強類型數據集的序列化

編輯:這裏是堆棧跟蹤:

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

+0

你能發佈你得到的異常堆棧跟蹤嗎? – 2012-02-25 13:09:04

+0

添加了上面的堆棧跟蹤 – bechbd 2012-02-29 17:42:36

+0

這篇SO文章可能是相關的: - http://stackoverflow.com/questions/7891447/how-to-serialize-de-serialize-a-custom-dataset – 2012-02-29 17:51:59

回答

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

從反序列化器中取回對象後,嘗試將其轉換回您的類型化數據集類型。

+0

這是一個強類型在Visual Studio中創建數據集,方法是添加數據集,然後將表格/ sproc拖放到設計圖面上。這似乎反直觀,我應該需要手動序列化/反序列化這種內置的Visual Studio功能。 – bechbd 2012-02-29 18:07:33

+0

我建議這樣做的原因是,如果你自己對二進制文件進行序列化,你可以刪除對appfabric客戶端的依賴性,檢查你的對象並試圖找出如何序列化它本身。例如,如果客戶端在類的任何成員上都找到WCF數據註釋屬性,則它將嘗試使用您可能不想執行的NetDataContractSerializer。 – 2012-02-29 18:10:14

0

您可以使用DataSet.ReadXML()或WriteXML()函數來實現所需的功能。