After upgrading to NHibernate 3.1 and .Net 4 I am seeing a strange exception in our web application. The error message may be familiar to many:
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.
Switching to "inProc" mode, everything works fine. But we cannot use inProc mode in production.
Now, the error only happens ONCE. Then goes away until the application is redeployed or restarted.
Below is a complete stack trace as it appears in the log. Please note the following interesting points:
- PersistentGenericBag`1[DAL.DTO.Cargo] may indicate that this is related to lazy loading
- The serializer is trying to convert it to an Int32
- When investigating the session contents, I can find absolutely no DTO objects, or references to any. If I Clear() the session, the exception goes away, but our application breaks.
[InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[DAL.DTO.Cargo]' to type 'System.IConvertible'.] System.Convert.ToInt32(Object value, IFormatProvider provider) +21 System.Runtime.Serialization.Formatters.Binary._BinaryWriter.WriteValue(InternalPrimitiveTypeE code, Object value) +62 System.Runtime.Serialization.Formatters.Binary._BinaryWriter.WriteMember(NameInfo memberNameInfo, NameInfo typeNameInfo, Object value) +76 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, Object data) +75 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMembers(NameInfo memberNameInfo, NameInfo memberTypeNameInfo, Object memberData, WriteObjectInfo objectInfo, NameInfo typeNameInfo, WriteObjectInfo memberObjectInfo) +198 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMemberSetup(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String memberName, Type memberType, Object memberData, WriteObjectInfo memberObjectInfo) +139 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String[] memberNames, Type[] memberTypes, Object[] memberData, WriteObjectInfo[] memberObjectInfos) +186 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +480 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +444 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +133 System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1708 <
[HttpException (0x80004005): 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.] System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1793 System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +34 System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +638 System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +244 System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +67 System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +114 System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +807 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75