c# – 在整个页面生命周期中,会话中存储的变量是一次还是多次反
发布时间:2020-12-15 18:28:43 所属栏目:百科 来源:网络整理
导读:我想以类似于 discussed on CodeProject的方式包装Session变量. public static class WebSession{ private const string CurrentUserKey = "CurrentUser"; private static HttpSessionState Session { get { return HttpContext.Current.Session; } } public
我想以类似于
discussed on CodeProject的方式包装Session变量.
public static class WebSession { private const string CurrentUserKey = "CurrentUser"; private static HttpSessionState Session { get { return HttpContext.Current.Session; } } public static bool Exists { get { return Session != null; } } public static User CurrentUser { get { return Session[CurrentUserKey] as User; } set { Session[CurrentUserKey] = value; } } } 这是我的问题:如果我必须在同一页面中多次访问CurrentUser,是否可以通过将其分配给本地变量而不是访问包装属性来提高性能?或者HttpSessionState是否确保每个请求只对对象进行一次反序列化,以便在同一个http请求中的后续调用不再花费多少? 谢谢, 解决方法
每个请求都有一个内存中的Session状态副本.因此,通过本地复制会话变量来节省的唯一成本是从Object到您的类型的转换.然后在请求结束时将内存中的副本添加到Session.
是否在页面上序列化和反序列化会话取决于您选择的会话提供程序.对于进程内会话状态,不会发生序列化.对于会话服务器,必须首先序列化对象. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |