0

セッションにアクセスするためのこのヘルパー クラスがありますが、最後の値しか保存されません。バグを検出できません。

public interface ISessionHelper
{
    void AddToSession<T>(string key, T value);
    T GetFromSession<T>(string key);
    void RemoveFromSession(string key);
}

public class SessionHelper : ISessionHelper
{
    private static SessionHelper sessionHelper;
    private Dictionary<string, object> sessionCollection;
    private const string SESSION_MASTER_KEY = "SESSION_MASTER";

    private SessionHelper()
    {
        if (null == sessionCollection)
        {
            sessionCollection = new Dictionary<string, object>();
        }
    }

    public static ISessionHelper Instance
    {
        get
        {
            if (null == sessionHelper)
            {
                sessionHelper = new SessionHelper();
            }
            return sessionHelper;
        }
    }

    public void AddToSession<T>(string key, T value)
    {
        sessionCollection[key] = value;
        HttpContext.Current.Session.Add(SESSION_MASTER_KEY, sessionCollection);
    }

    public void RemoveFromSession(string key)
    {
        sessionCollection.Remove(key);
    }

    public T GetFromSession<T>(string key)
    {
        var sessionCollection = HttpContext.Current.Session[SESSION_MASTER_KEY] as Dictionary<string, object>;
        if (null != sessionCollection)
        {
            if (sessionCollection.ContainsKey(key))
            {
                return (T)sessionCollection[key];
            }
            else
            {
                return default(T);
            }
        }
        else
        {
            throw new Exception("Session Abandoned");
        }
    }
}

このクラスへの呼び出しは、次のように行われます。

SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_INFO, user);

クラスの辞書部分の初期化が問題を引き起こしていると思いますが、特定できません。

または、これが問題になる可能性がありますか?

void Session_Start(object sender, EventArgs e)
{
    CUserRoles userRoles;

    string currentUser = HttpContext.Current.User.Identity.Name;

    User user = UserManager.GetUserCompleteInfo(currentUser, out userRoles);

    if (user != null && user.UserID > 0)
    {
        SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_INFO, user);

        if (userRoles != null)
        {
            SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_ROLE, userRoles);
        }
    }
    else
    {
        Response.Redirect("AccessDenied.aspx", true);
    }
}
4

1 に答える 1

0

sessionCollectionシングルトンのメンバーであるため、役割があると思います。メンバーを削除しsessionCollectionてセッションから取り出し、それを使用するか、必要に応じて各メソッドに挿入します。あなたGetFromSessionは正しいパターンのように見えます。

より簡単な方法は、これを行うことです。コレクションを取得するには、これを呼び出します。

private Dictionary<string, object> GetSessionCollection() 
{ 
     // add locking
    var collection = HttpContext.Current.Session[SESSION_MASTER_KEY] as Dictionary<string, object>;

    if (collection == null) 
    {
        collection = new Dictionary<string, object>()
        HttpContext.Current.Session[SESSION_MASTER_KEY] = collection;
    }

    return collection;
}

更新されたAddToSession

public void AddToSession<T>(string key, T value)
{
    var sessionCollection = GetSessionCollection();
    sessionCollection.Add(key, value);
}
于 2013-03-27T16:16:40.280 に答える