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

Unable to Serialize the Session State Error in Asp.net

83
1
unable to serialize the session state
Unable to Serialize The Session State 2

When switching the sessionState from InProc mode to SQLServer mode, you might get an 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 1
Unable to Serialize The Session State 1

Server Error in ‘/’ 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.

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 2
Unable to Serialize The Session State 2

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 an 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.

Previous articleSelect GridView Rows by Check Uncheck CheckBox in jQuery
Next articleCSS Selectors in Style Sheet
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

16 − 11 =