Question

I'm currently working on implementing the new ASP.NET MVC 5 out-of-the box authentication into my application. However when using Unity as my IoC, I cannot use any portion of the AccountController because I'm given the error:

The type IUserStore`1 does not have an accessible constructor.

This is my given unity setup which is called in the global.asax

public class DependencyConfig
{
    public static void Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        container.RegisterType<IEmployeeRepository, EmployeeRepository>();

        container.RegisterType<ITeamRepository, TeamRepository>();

        container.RegisterType<ICompanyRepository, CompanyRepository>();

        return container;
    }
}

And here are the default constructors of a fresh AccountController.cs

public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new BusinessTrackerUsersContext())))
{

}

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

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

And here are the items being called in the AccountController constructors. These are the defaults with new names.

public class BusinessTrackerUsersContext : IdentityDbContext<ApplicationUser>
{
    public BusinessTrackerUsersContext()
        : base("DefaultConnection")
    {

    }
}

public class ApplicationUser : IdentityUser
{

}

Any help would be widely appreciated!

Was it helpful?

Solution

Because you have two constructors on your controller, unity will pick up the one with longer parameter list, the latter. It requires the UserManager to be injected.

Although you haven't listed it here, I suspect is has an only constructor that requires the IUserStore. Unity tried to resolve it and can't find any constructor it could use directly or resolve its parameters.

OTHER TIPS

I agree with Wiktor.

You could register the parameterless constructor with Unity though and stop it taking the longer parameter by doing this:

container.RegisterType<AccountController>(new InjectionConstructor());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top