Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get a working Unity Session Lifetime Manager, ASP.NET MVC5

I've read and Googled everything on this, but can't seem to get it to work. I created a custom LifetimeManager for Unity in my MVC5 application based on these posts:

  • MVC3 Unity Framework and Per Session Lifetime Manager
  • This may be the issue I am experiencing

Here is my SessionLifetimeManager

public class SessionLifetimeManager : LifetimeManager
{
    private string key = Guid.NewGuid().ToString();

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

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

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

I only have a few types I'm playing with, here is the relevant registrations in UnityConfig.cs:

container.RegisterType<IEpiSession, EpiSession>(new SessionLifetimeManager(), 
    new InjectionConstructor(config.AppServerURI, config.PathToSysConfig));
container.RegisterType<IReportRepository, EpicorReportRepository>(new TransientLifetimeManager());

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

Note that the EpicorReportRepository has a dependency on IEpiSession via constructor injection.

public class EpicorReportRepository : IReportRepository
{
    private IEpiSession session;

    // DI constructor
    public EpicorReportRepository(IEpiSession session) {
        this.session = session;
    }
// ...
}

My Problem: After the first user / session connects to the application, every new user / session after that seems to still be using the EpiSession object and credentials that the first user had create/injected for him. This seems to be a common pattern used on the interwebs, so I'm wondering what I am missing.

like image 400
Adam Nofsinger Avatar asked Nov 10 '22 12:11

Adam Nofsinger


1 Answers

How did you test that IEpiSession is the same in different Sessions?

Try to open you application from different browsers. If you open several tabs in the same browser then the same session is used.

I checked your code and it works for me. There is the only one difference in SetResolver():

DependencyResolver.SetResolver(
    type => container.Resolve(type),
    types => container.ResolveAll(types));

The full registration code is the following:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...
        var container = new UnityContainer();
        container.RegisterType<IEpiSession, EpiSession>(
            new SessionLifetimeManager(),
            new InjectionConstructor("config.AppServerURI", "config.PathToSysConfig"));
        container.RegisterType<IReportRepository, EpicorReportRepository>(new TransientLifetimeManager());

        DependencyResolver.SetResolver(
            type => container.Resolve(type),
            types => container.ResolveAll(types));
    }
}
like image 157
Ilya Palkin Avatar answered Nov 15 '22 07:11

Ilya Palkin