Are Linq for sql objects serializable for session state?

Without going into it, a good or bad idea:

Is it possible to save a LINQ-to-SQL domain object in an ASP.NET session when the session is down?

[EDIT] I am currently getting the following error and asked this question because I suspect LINQ-to-SQL objects:

Unable to serialize session state. In StateServer and SQLServer mode, ASP.NET will serialize session state objects, and as a result, non-serialization objects or MarshalByRef objects are not allowed. The same restriction applies if similar serialization is performed in the user session state store in the "User" mode. [/ EDIT]

eg.

Session["Zoo"] = new Zoo() { new Lion(), new Tiger(), new Elephant() } 

Where:

  • Zoo, Lion, Tiger, Elephant all from ZooDataContext

and the web.config file contains

 <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" stateNetworkTimeout="10" sqlConnectionString="SqlStateConnectionString" sqlCommandTimeout="30" timeout="20" regenerateExpiredSessionId="true"/> 
+4
source share
3 answers

Serialize them using the datacontractserializer before saving to a session or anything else that may be required for serialization ... Recently discussed here :

http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/81c84ff4-059b-474f-9c69-b8c59027fd48

+1
source

To use a binary formatter (e.g. using SessionState, I believe) you will need to generate your code yourself from DBML (I am currently doing this with the Linq2Sql T4 template).

The following should be marked as [NonSerialized] :

  • EntityRef
  • EntitySet
  • All events (you will need to think outside the box, reading exercise)

Also, the designer logic needs to be moved to OnCreated . You must also ensure that OnCreated is called upon deserialization so that the object can be useful again. Executed with the [OnDeserializing] attribute.

+4
source

I would believe that you would need to mark your objects as serializable. I'm not sure if there is a way to do this for all generated objects, but for those that you invest in the session, you can create a partial class (like Lion) and assign it the Serializable attribute.

+1
source

All Articles