Session is Null with SQL Server Session Mode
The Problem
Session variable works fine with InProc Session Mode but is null when you switch the Session Mode to SQLServer mode.
You may see the following exception.
"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."
Make sure you check the inner exception to see which class needs to be changed to Serializable.
Review all the data you have stored in Session variables to verify the data is serializable. .NET knows how to automatically serialize the simple built in types like string, enum, integer, etc. Any classes you create are NOT serializable by default. Fortunately, you can make your classes serializable with a simple class attribute of [Serializable] like this.
[Serializable]
public class
Address
{
public int nStreetNum;
public string strStreetName;
}
[Serializable]
public class Customer
{
public string strFirstName;
public string strLastName;
public
Address Addr;
// The Address class must be serializable.
public Customer(string FirstName, string LastName, Address theAddr)
{
strFirstName = FirstName;
strLastName = LastName;
Addr = theAddr;
}
}
Your class must meet these requirements to be serializable:
- Your class must have the Serializable attribute
- Any classes it derives from must have the Serializable attribute.
- All class member variables must be serializable types. For example, the Address type used for Customer Addr member variable must also be serializable.
Visit www.BlueCanyonSoftware.com if you need
custom software development.