SessionState SQLServer Error

P

Patrick Nolan

(C#)
I am attempting to change the way I handle my session variables from
"Inproc" to "SQLServer".

After following the directions on Micorsoft's site, I now get this error
when running my project:

"Unable to serialize the session state. Please note that non-serializable
objects or MarshalByRef objects are not permitted when session state mode is
'StateServer' or 'SQLServer'"

The Stack trace is as follows:

[SerializationException: The type System.Data.SqlClient.SqlConnection in
Assembly System.Data, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 is not marked as serializable.]

System.Runtime.Serialization.FormatterServices.InternalGetSerializableMember
s(RuntimeType type, Boolean excludeNonSerializable) +853

System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
type, StreamingContext context) +300

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInf
o() +103

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize
(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter) +493

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Obj
ect obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter) +48

System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +683

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Str
eam serializationStream, Object graph, Header[] headers, Boolean fCheck)
+136

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Str
eam serializationStream, Object graph) +13
System.Web.Util.AltSerialization.WriteValueToStream(Object value,
BinaryWriter writer)

[HttpException (0x80004005): Unable to serialize the session state. Please
note that non-serializable objects or MarshalByRef objects are not permitted
when session state mode is 'StateServer' or 'SQLServer'.]
System.Web.Util.AltSerialization.WriteValueToStream(Object value,
BinaryWriter writer)
System.Web.SessionState.SessionDictionary.Serialize(BinaryWriter writer)
System.Web.SessionState.StateClientManager.Serialize(SessionStateItem
item, Stream stream)

System.Web.SessionState.SqlStateClientManager.System.Web.SessionState.IState
ClientManager.Set(String id, SessionStateItem item, Boolean inStorage)
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source,
EventArgs eventArgs)
System.Web.SessionState.SessionStateModule.OnEndRequest(Object source,
EventArgs eventArgs) +44

System.Web.SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.
Execute() +60
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously) +87


I have the [Serialization] statement in my code when defining the page
class as directed by Microsoft and various other web sites.

The Serialization error seems to focus on my SQL Connection.

Any ideas to get me in the right direction are apprectiated.
-Patrick

(e-mail address removed)
 
A

Abhijeet Dev

Exception simply means you are trying to store an instance of SqlConnection
in session, which i dont think would be required in any circumstances. The
way to do it is to mark all the instances of SqlConnection with
System.NonSerializedAttribute in all the classes that you have marked as
serialized.
When you declare a class as serializable, just have a look at the fields
which cannot be serialized and mark it as NonSerialized.

Abhijeet Dev
 
A

Ashish

Patrick said:
(C#)
I am attempting to change the way I handle my session variables from
"Inproc" to "SQLServer".

After following the directions on Micorsoft's site, I now get this error
when running my project:

"Unable to serialize the session state. Please note that non-serializable
objects or MarshalByRef objects are not permitted when session state mode is
'StateServer' or 'SQLServer'"

The Stack trace is as follows:

[SerializationException: The type System.Data.SqlClient.SqlConnection in
Assembly System.Data, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 is not marked as serializable.]

System.Runtime.Serialization.FormatterServices.InternalGetSerializableMember
s(RuntimeType type, Boolean excludeNonSerializable) +853

System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
type, StreamingContext context) +300

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInf
o() +103

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize
(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter) +493

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Obj
ect obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter) +48

System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +683

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Str
eam serializationStream, Object graph, Header[] headers, Boolean fCheck)
+136

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Str
eam serializationStream, Object graph) +13
System.Web.Util.AltSerialization.WriteValueToStream(Object value,
BinaryWriter writer)

[HttpException (0x80004005): Unable to serialize the session state. Please
note that non-serializable objects or MarshalByRef objects are not permitted
when session state mode is 'StateServer' or 'SQLServer'.]
System.Web.Util.AltSerialization.WriteValueToStream(Object value,
BinaryWriter writer)
System.Web.SessionState.SessionDictionary.Serialize(BinaryWriter writer)
System.Web.SessionState.StateClientManager.Serialize(SessionStateItem
item, Stream stream)

System.Web.SessionState.SqlStateClientManager.System.Web.SessionState.IState
ClientManager.Set(String id, SessionStateItem item, Boolean inStorage)
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source,
EventArgs eventArgs)
System.Web.SessionState.SessionStateModule.OnEndRequest(Object source,
EventArgs eventArgs) +44

System.Web.SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.
Execute() +60
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously) +87


I have the [Serialization] statement in my code when defining the page
class as directed by Microsoft and various other web sites.

The Serialization error seems to focus on my SQL Connection.

Any ideas to get me in the right direction are apprectiated.
-Patrick

(e-mail address removed)

you cannot serialize SQLConnection objects, it is not a good practise
yoiu can easily work your way around it by putting <NonSerializable> in
front of the declaration of the SQLConnection object, but make sure to
open it everytime you try access the database, and close it immideatly
after the db access is done

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top