Search code examples
c#asp.netasp.net-mvcdependency-injectionunity-container

mvc5: Cannot access a disposed object. asp.net mvc5 using unity


im using vs 2013, mvc5 and ef6 + unity.mvc5: my account controller class has this constructor:

public AccountController(UserManager<ApplicationUser> userManager)
    {
        this.UserManager = userManager;
    }

and this is my unity config:

    container.RegisterType(typeof(AccountController),
            new InjectionConstructor(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>())));

when i try to register a new user i get this exception:

Cannot access a disposed object.
Object name: 'UserManager`1'.

on this line of code and register action:

var result = await UserManager.CreateAsync(user, model.Password);

when i remove unity and set dependecy resolver to default it works just fine. i`v tried many unity configs but didnt work... thanks alot for reading.


Solution

  • i found the solution, changed the unity config to this:

    container.RegisterType<IUserStore<ApplicationUser>, 
                UserStore<ApplicationUser>>(new InjectionConstructor(new ApplicationDbContext()));
    

    thanks all.