会话数据的序列化和反序列化

会话数据的序列化和反序列化

问题描述:

我已经使用asp.net在SQL Server模式下存储了会话数据,因此存储的数据由asp.net序列化了,我正在使用一个反序列化数据并将其返回为对象类型的函数,现在我将如何检索数据?
我创建了一个可序列化的类,使用该类我将会话数据存储在表中。

I have stored session data in SQL server mode using asp.net.The data thus stored is serialized by asp.net.I am using a function to deserialize the data and return it in an object type.Now how will I retrieve the data? I had created a class,a serializable one,using which i am storing session data in the table.

任何与此相关的帮助都将非常有帮助。

Any help regarding this will be very helpful.Thanks

您不需要手动反序列化以ASP.NET会话状态存储的对象。如果您已将会话状态配置为:

You don't need to manually deserialize objects stored in ASP.NET session state. If you have session state configured like this:

  <configuration>
    <system.web>
      <sessionState mode="SQLServer" sqlConnectionString="..." />
    </system.web>
  </configuration>

ASP.NET将自动为您序列化和反序列化它。
因此,为了检索数据,只需从会话对象中读取它:

ASP.NET will automaticaly serialize and deserialize it for you. So in order to retrieve the data just read it from the session object:

Session["MyKey"] = new MyClass();
var myData = (MyClass)Session["MyKey"];