Serialization error with SqlConnection variable

I'm getting a serialization error in ASP.NET when it tries to store one of my classes in the Session state. I marked the SqlConnection variable as non-serializable but it still tries to store it in the session state. My class looks like this:

[Serializable]
public class SqlServerDAL {
[XmlIgnoreAttribute]
private SqlConnection Connection;
}

Any ideas why this is happening even though I marked it with the XmlIgnoreAttribute

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

SerializationException: Type 'System.Data.SqlClient.SqlConnection' in Assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +2317461
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +245



Answer this question

Serialization error with SqlConnection variable

  • Noldona

    Hi,

    You marked this attribute to be "ignored" with XML serialization. But StateServer or SQLServer state use Binary Serialization....

    SQLConnection cannot be serialized neither using XMLFormatter nor using BinaryFormatter.... so you can't put it in StateServer or SQLServer

    In order to achive what you want you will have to implement ISerializable in the SqlServerDAL class so you can "customize" its serialization logic.

    Rgds,

    Rodrigo


  • sanjay tiwari

    SQLConnections are not serializable.

    What you need to do is mark your object as serializable, and derive from IDeserializationCallback.

    Also mark the variable holding your connection as NonSerialized, like ...

    [Serializable]

    class SerializePlay : IDeserializationCallback

    {

    [NonSerialized]

    System.Data.SqlClient.SqlConnection foo = null;

    The you need to implement the OnDeserialization method of IDeserializationCallback. This method is called during deserialization after all of the serializable members have been reconsitituted.

    void IDeserializationCallback.OnDeserialization(object sender)

    {

    //recreate your connection here

    }

    You will need to add a reference to System.Runtime.Serialization.

    Also search for IDeserializationCallback on MSDN.Microsoft.com to see another example.



  • TedViste

    Do not store SqlConnection objects in session state.

    It will be faster to recreate the sqlconnection object for each data acess request than it would be to retrieve such a class from session or even cache.

    If each user will have a different connection store the connectionstring only in Session.


  • Serialization error with SqlConnection variable