0
votes

Does anyone know a way to make a strongly typed dataset (Hey not my idea to use them) serializable so that I can store them using AppFabric Server as a Session State provider? I have been using InProc Session state for some time now and haven't had any issues with this. As soon as I moved the Session state out to AppFabric Server I am getting some issues with strongly typed datasets. I keep getting an error telling me that my datasets aren't serializable. I have checked and they have the Serializable attribute on them and they only contain serializable data types (int, string, etc.) Any thougnts?

Edit: Here is the Stack Trace:

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
Can you post the exception stack trace you're getting? - Phillippe Francois
This SO article may be relevant:- stackoverflow.com/questions/7891447/… - Nick Ryan

2 Answers

1
votes

Try serializing / deserializing the object to a byte array first before you attempt to put it into session (and thence to AppFabric). Here is an example.

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

After you get the object back from the deserializer attempt to cast it back to your typed dataset type.

0
votes

You can use DataSet.ReadXML() or WriteXML() functions to achieve desired functionality.