Home Asp.net Unable to Serialize the Session State Error in Asp.net

Unable to Serialize the Session State Error in Asp.net

25
1

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

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

unable to serialize the session state error deduction

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

//To serialize “CommonVariables” class write following statement
[Serializable]
public class CommonVariables
{
//Some code here
}
Tip: You can get more tutorials on Asp.net, JavaScript, jQuery, IIS and other Solutions 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.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!