In a simple word, I'm trying to create a Lifetime manager for the Unity framework using the Http Session in my MVC3 project. My sample lifecycle manager implementation:
public class UnityPerSessionLifetimeManager : LifetimeManager { private string sessionKey; private HttpContext ctx; public UnityPerSessionLifetimeManager(string sessionKey) { this.sessionKey = sessionKey; this.ctx = HttpContext.Current; } public override object GetValue() { return this.ctx.Session[this.sessionKey]; } public override void RemoveValue() { this.ctx.Items.Remove(this.sessionKey); } public override void SetValue(object newValue) { this.ctx.Session[this.sessionKey] = newValue; } }
In my global.asax.cs, I replaced the factory default controller with my own UnityControllerFactory
public class UnityControllerFactory : DefaultControllerFactory { private IUnityContainer container; public UnityControllerFactory(IUnityContainer container) { this.container = container; this.RegisterServices(); } protected override IController GetControllerInstance(RequestContext context, Type controllerType) { if (controllerType != null) { return this.container.Resolve(controllerType) as IController; } return null; } private void RegisterServices() { this.container.RegisterType<IMyType, MyImpl>(new UnityPerSessionLifetimeManager("SomeKey")); } } }
I set breakpoints for each function of the UnityPerSessionLifetimeManager class, I noticed that when the factory controller tries to solve the dependencies of my controller, the HttpContext.Session is actually null, so the code is not obtained from the session or saved to the session.
Any idea why the session at this stage is zero?
source share