SQL Server Session State causing Serialization error

session-statesql server

I have an .Net 3x application using default WCF serialization and recently setup application to use SQL Server Session state and am not being able to store those objects in session state with sql server, only in proc session state.

Getting errors on various classes stating:

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.

I assumed that the default serialization of wcf would be sufficient but apparently not. Any ideas on what needs to be done to correct this short of editing entire app and adding [Serializable] to classes?

Best Answer

It seems like you have four choices:

  1. Switch back to InProc mode
  2. Make your objects [Serializable]
  3. Instead of storing high-level container objects in the Session dictionary, use lower-level objects instead, that are already serializable (such as strings, ints, etc)
  4. Avoid using traditional session state, and switch to some alternative state management method instead

Option 3 will generally run faster than 2, although it's also more work.