Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode...


Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode...











Solution:
Normally this is because you are running using a stateserver or SqlServer instead of inproc for the session state. But, you'd get the error consistently if that was the case. Maybe it's an object only get’s stored to the session occasionally?
I've run into situations where I didn't get the error until the end of the page generation. So it may be hard to track down exactly which object you are putting into the session that needs to be marked as serializable. You'll need to mark the class serializable once you find it.
Check all objects that you are storing in session. One of your objects is exposing an object as public property or field which is non serializable. An example could be Thread object which is not serializable.
If there is something that does not need to be serialized, make them non-public or decorate then with Non-serializable attribute. You will have to look at your objects closely to make the decision and if there is something that needs to be serializable and by default does not do it by itself then you will have to manually serialize those objects. Also put [Serializable] attributes on your objects.
Note: some object can not be serialized, such as SqlConnections, DataReaders, Streams, com Interop objects, Collections containing a non-serializable object, etc. in this case you must be redesign you session data.


Comments