views:

112

answers:

3

I'm getting the following error in my application:

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.

The stack trace isn't providing any good information on which object is not able to serialize. Is there a good way to find the problem child?

Edit: I've found the issue, I was trying to serialize a Linq statement(not executed). But I'll try an choose an answer that would have best solved this issue.

+3  A: 

Really, you should mainly be storing your own state data / objects (ideally modelled as DTO classes), in which case the answer is: any you mark as [Serializable] or ISerializable. You shouldn't be adding raw UI controls or other unknown objects to session-state. In particular, for reasons like this, which had a major performance impact on an app the other day.

Marc Gravell
+1 Good point and informative link.
IrishChieftain
@Marc Gravell: I thought I was storing an IEnumerable<KeyValuePair<string, int>>, the problem was I was only storing the Linq clause, but in other places I was first using the ToList() extension to enumerate.
Yuriy Faktorovich
A: 

The best I could do in a similar situation is to look at every object the Session references and check it for the presence of the Serializable attribute (or that the object implements ISerialzable interface).

mfeingold
+2  A: 

MbUnit (now Gallio) has an Assert.IsSerializable() test that could come in handy here.

Wyatt Barnett
I did create a unit test which did a binary serialization on the class I thought was the culprit.
Yuriy Faktorovich