Friday, August 12, 2016

Be careful when switching to use mongoDB session state provider

We just upgraded our Sitecore to use xDB.  And because we have a couple of front end servers that are load balanced, we switched our web.config from using the InProc sesstionState mode to using the custom MongoDB sessionState.

Before you do this, make sure that you check your code for any use of the Session object... typically some reference like HttpContext.Current.Session["Key"] = object.

If you are saving an object to the session, you'll need to make sure that object has been tagged as [Serializable].

Any that you miss will not work correctly when you deploy that config change.  And you'll see a bunch of errors in the event log:

    Exception type: SerializationException 
    Exception message: Type 'My.CustomObject' in Assembly 'My.Website, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer)

1 comment:

  1. I see that this has already been warned about for similar reasons on the briancaos Wordpress site: https://briancaos.wordpress.com/2014/03/07/unable-to-serialize-the-session-state-in-stateserver-and-sqlserver-mode

    ReplyDelete