MVC3, Unity Framework and Per Session Lifetime Manager Issue

送分小仙女□ 提交于 2019-12-01 06:42:15

My mistake, I should change code of UnityPerSessionLifetimeManager class to be

public class UnityPerSessionLifetimeManager : LifetimeManager
{
    private string sessionKey;

    public UnityPerSessionLifetimeManager(string sessionKey)
    {
        this.sessionKey = sessionKey;
    }

    public override object GetValue()
    {
        return HttpContext.Current.Session[this.sessionKey];
    }

    public override void RemoveValue()
    {
        HttpContext.Current.Session.Remove(this.sessionKey);
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Session[this.sessionKey] = newValue;
    }
}

because when the constructor was called to register type, session state is not ready yet and I already assigned http context of that time to a variable. But in later Get/Set functions session state is ready.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!