When switching the sessionState mode of your web project from InProc to SQLServer you might encounter this error:
Exception: System.Web.HttpException
Message: 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.
…
…
.NET handles the storage of session objects differently from InProc to SQLServer. When storing session objects in a SQL Server, .NET will serialize the objects. This is necessary because the session object needs to be transferred from server to server.
To support this, all you need is to mark the objects that are part of your sessions with the [Serializable] attribute:
namespace MyNameSpace { // Class is marked as serializable [Serializable] public class MyClass { // Some code here } }
So how do you find the classes that need the [Serializable] attribute?
.NET is pretty good at telling which classes needs to be serializable in the nested exception:
Nested Exception
Exception: System.Runtime.Serialization.SerializationException
Message: Type ‘MyNamespace.MyClass‘ in Assembly ‘MyAssembly, Version=4.0.4.18681, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.
Source: mscorlib
…
…
MORE TO READ:
- HOW TO: Configure SQL Server to Store ASP.NET Session State by Microsoft
- Unable to serialize the session state post from StackOverflow
NOTE TO SITECORE DEVELOPERS:
Sitecore does not as such support other modes than InProc session state. As Sitecore states (quote from the Sitecore Scaling Guide):
The Sitecore CMS user interfaces require in-process ASP.NET session management. For CM
instances, the value of the mode attribute of the /configuration/system.web/sessionState
element in the web.config file must be InProc.
In-process session management requires you to configure the CM load balancer for server affinity —
also known as sticky sessions. You can use other values for the mode attribute in the CD
environment.
This means that the Sitecore client and page editor is not tested using other session state methods than InProc. But since the content delivery servers do not touch the Sitecore UI, it is up to you to make the front end code compatible with StateServer or SQLServer methods.
The best session state management for Sitecore CD servers would still be InProc, and let your load balancer use sticky sessions.
We just turned on 8.1 xDB and ran into this problem. After changing to use the mongodb private sessionstate provider, our site was broken in places that used the session object. We have code that saves custom class objects to the session, and those weren’t marked serializable.
LikeLike
Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog