0
votes

I want to store my session in SQL Server database and for that I want to use session state mode SQLSERVER, and I have changed my web.config and added the session state attribute:

<sessionState mode="SQLServer" timeout="30" sqlConnectionString="Data Source=server;user id=sa;password=sa;" cookieless="false" />

When I run my application I get this 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.

Please help me, I am in serious problem. Thanks & Regards.

1
What is your code? What you try putting into Session? Like the error states, you can't put any object. - Shadow Wizard Wearing Mask V2
actually i am creating demo application containing simple aspx page and on code behind there is nothing,it's blank. - Anil
Something, somewhere, is assigning something to the Session.. can you post the full Stack Trace of the exception? - Shadow Wizard Wearing Mask V2
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. - Anil
No, that's only the message. Below this you should see some lines which are known as stack trace and telling where the error is coming from. Please copy those lines and edit your question with them. - Shadow Wizard Wearing Mask V2

1 Answers

0
votes

The question is old but had no valid answer, I had same problem and found the solution on another site. Since this topic comes as first result on google I am adding the solution here for others.

The issue is about adding a struct object into session. 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.

Stack trace (yellow section on the error page) shows exactly which object caused the problem. In my case:

[SerializationException: Type 'admin+users+stUsers' in Assembly 'App_Code.3_0rnsep, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.]

I added [Serializable] tag ebove my struct and the problem was gone.

[Serializable]
public struct stUsers
{ .... }