【问题标题】:Can Sessions conflict when encapsulated in a static class static property?封装在静态类静态属性中时会话会发生冲突吗?
【发布时间】:2014-12-22 20:54:37
【问题描述】:

我有一个 ASP.NET 应用程序,平均有 120-140 个用户同时访问。我使用 Session 来获取和设置用户特定的信息。为了方便起见,我有一个名为CurrentSession 的静态类,它有一个名为UserInfo 的属性:

public static class CurrentSession{
     public static UserInfo{
          get{return HttpContext.Current.Session["userInfo"]!=null?(UserInfo)HttpContext.Current.Session["userInfo"]:null;}
          set{HttpContext.Current.Session["userInfo"]=value;}
     }
    //And other properties
}

每当我需要当前用户的信息时,我都会这样做:

CurrentSession.UserInfo;

最近我遇到了检索错误用户信息的情况。我的方法中是否存在可能导致Session 冲突的问题?

【问题讨论】:

    标签: c# asp.net .net session


    【解决方案1】:

    没有。不可能是static方法引起的会话变化。事实上,HttpContext.Current 本身就是一个static。将其分配给静态变量可能会导致这种情况。

    【讨论】:

    • @Farid:您确认问题的根源了吗?
    • 亲爱的@Patrick Hofman,我也这么认为,并且确信情况并非如此。我只是想确保我的方法没有问题。事实上,代码没有问题。这是一个人代表许多其他人访问该应用程序的结果。所以这不是冲突。
    • @FaridImranov:好的。很高兴你找到它。
    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多