简体   繁体   English

asp.net 中的 session 架构

[英]session architecture in asp.net

I have an object that looks like this:我有一个看起来像这样的 object:

public class MySession{

   string UserID {get;set;}
   List<Object1> ListOfObject1 {get;set;}
   List<Object2> ListOfObject2 {get;set;}
   ....
   several other lists....
}

The advantage of doing this is that I can write something like "on page load" MySession TheSession = Session["TheSession"] as MySession;这样做的好处是我可以写类似“页面加载” MySession TheSession = Session["TheSession"] as MySession; and then access the session's properties more easily in code.然后在代码中更轻松地访问会话的属性。

For now, this works with InProc session but I'm looking to move to SQL Server session.目前,这适用于 InProc session,但我希望转移到 SQL 服务器 session。

What's the best way to make this change?进行此更改的最佳方法是什么? I'm thinking of serializing the MySession object into a json string and then letting SQL Server session save the session as a string. I'm thinking of serializing the MySession object into a json string and then letting SQL Server session save the session as a string. Then, when I reload the session, simply load the json string and deserialize it.然后,当我重新加载 session 时,只需加载 json 字符串并反序列化它。

I don't know if this is the most efficient way to do it.我不知道这是否是最有效的方法。 Thanks for your suggestions.感谢您的建议。

All you need to do is make MySession and all related classes [Serializable] (binary serialization).您需要做的就是制作MySession和所有相关的类[Serializable] (二进制序列化)。

ASP.Net will automatically serialize it for you. ASP.Net 会自动为您序列化它。

If efficiency is the only concern, I am not sure you gain much, unless the object graph is fairly large on some users.如果效率是唯一的问题,我不确定你会获得多少,除非 object 图对某些用户来说相当大。 With session defaults, it is binary serialized, which would pack down even more than your JSON.使用 session 默认值,它是二进制序列化的,这将比您的 JSON 打包更多。

If you need some readability on the session bits (debugging?), then you could go your route.如果您需要 session 位的可读性(调试?),那么您可以 go 您的路线。

As long as you mark your class with [Serializable] attribute and it is in fact serializable, you don't have to do your own manual serialization.只要你用[Serializable] 属性标记你的 class 并且它实际上是可序列化的,你不必自己手动序列化。 So long as your web application is configured properly for out-of-process session state management and everything you stick into Session is serializable, everything should work. So long as your web application is configured properly for out-of-process session state management and everything you stick into Session is serializable, everything should work.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM