How can I find which object in ASP.NET cannot be serialized?

The following error appears in my application:

Unable to serialize session state. In the "StateServer" and "SQLServer" modes, ASP.NET will serialize the session state objects and, as a result, non-serializable objects or MarshalByRef objects are not allowed. The same restriction applies if similar serialization is done by the user repository of session states in the "User" mode.

The stack trace does not provide any good information about which object cannot be serialized. Is there a good way to find a problem with a child?

Edit: I found a problem, I tried to serialize the Linq statement (did not execute). But I will try to choose the answer that would best solve this problem.

+6
serialization asp.net-mvc session
source share
5 answers

Indeed, you should mainly store your data your own data / state objects (ideally modeled as DTO classes), in which case the answer is this: either you mark it as [Serializable] or ISerializable . You should not add raw user interface controls or other unknown objects to the session state. In particular, for reasons like this , that the other day had a significant impact on the application.

+5
source share

MbUnit (now Gallio) has an Assert.IsSerializable () test, which may come in handy here.

+2
source share

You can try to serialize the object and System.Runtime.Serialization.SerializationException , which is raised when it is not serialized. It should tell you which member or parent cannot be serialized.

Code example:

 var f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); f.Serialize(new System.IO.MemoryStream(), yourObject); 
+1
source share

The best thing I could do in such a situation is to look at each object referenced by Session and check for the presence of the Serializable attribute (or that the object implements the ISerialzable interface).

0
source share

You can find the types of culprits that trigger serialization exception by debugging .net framework code. At least what I did.

0
source share

All Articles