MVC3, Unity Framework and Per Session Lifetime Manager Issue

后端 未结 1 1702
梦谈多话
梦谈多话 2021-01-13 20:06

In a simple word I try to create Lifetime manager for Unity framework by using Http Session in my MVC3 project. My sample implementation of lifetime manager is:



        
相关标签:
1条回答
  • 2021-01-13 21:06

    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.

    0 讨论(0)
提交回复
热议问题