Unable to serialize the session state 'System.Web.SessionState.HttpSessionState'

0

Getting "Unable to serialize the session state" error, where I'm trying to store HttpSessionState object in Session. Below is my code which is causing issue. Since HttpSessionState is not serializable class and trying to put into session.

Public Shared Property CurrentSession As HttpSessionState
    Get
        Return TryCast(HttpContext.Current.Session("CurrentSessionObject"), HttpSessionState)
    End Get
    Set(value As HttpSessionState)
        HttpContext.Current.Session("CurrentSessionObject") = value
    End Set
End Property

Server Error in '/' Application.

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.

Exception Details: System.Web.HttpException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SerializationException: Type 'System.Web.SessionState.HttpSessionState' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.] System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +12672675 System.Runtime.Serialization.<>c__DisplayClass9_0.b__0(MemberHolder _) +42 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) +73 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +186 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +166 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +187 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +53 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +571 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +137 System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1644

[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) +1733 System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +36 System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +652 System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +246 System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +65 System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +138 System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +589 System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs) +9947060 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +144 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +73

asp.net
vb.net
serialization
web-config
session-state
asked on Stack Overflow Apr 24, 2021 by Praveen Mitta

1 Answer

0

You need to mark the type/s your pushing in to SessionState as serializable, with the [Serializable] attribute.

answered on Stack Overflow May 6, 2021 by benmccallum

User contributions licensed under CC BY-SA 3.0