When switching the sessionState from InProc mode to SQLServer mode, you might get error of unable to serialize the session state as shown below image:
Sometimes we need to switch sessionState from InProc mode to SQLServer mode to store session objects into a SQL Server.
Unable to Serialize the Session State Error
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.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: 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.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
ASP.NET web application handles the storage of session objects differently from InProc mode to SQLServer mode.
When storing session objects into a SQL Server, .NET framework will serialize the objects. This is necessary because the session object needs to be transferred from server to server.
Trouble to find which class needs to be serialized? 😉
Don’t worry; it’s very easy to find out actual class that causing error. Please check the below image, in that I marked the actual class that is causing me.
Error Indication
Here, “CommonVariables” class in “DiscaShare.Web” assembly was causing me this error.
This error states that there is something being serialized that is not marked as Serializable. Try setting the [Serializable] tag on your class.
Unable to Serialize the Session State Error Solution
[Serializable]
public class CommonVariables
{
//Some code here
}
Bonus – Error Indication
Also note that this error can come from different reasons. Here are two other possible reasons that may or may not cause you unable to serialize error:
- Check all the objects that you are using in that class, and remove the objects that are non-serializable
- Also if you are using HttpResponse object then remove it from that class because that object is non-serializable and find out alternate method to pass response
Note: You can also find the class that needs to be serializable by traversing the code or nested exceptions. Mostly that class contains session objects under it.
Hi dear
my non-serialize class in XmlDocument and I cannot Set [Serializable] Attribute.
do you have any solution?