Posted on Jan 26, 2015 by Morten Empeño
Setting up Unity in an MVC application with authentication may cause the error: The type IUserStore`1 does not have an accessible constructor This blog describes why and how to fix it.
This morning I was setting up Unity on a MVC application. This was all done for the purpose of dependency injection and for being able to fully unit test my application. Unity is easy to set up and configure, so I will probably write a blog about setting up Unity in a MVC application as soon as possible.
Well… after implementing Unity everything seemed to work, until I tried to access the default account/login page. To my surprise I was having an error thrown in my face, which I did not see before:
“The type IUserStore`1 does not have an accessible constructor”
Looking into the stacktrace, it was proven to be caused by Unity. I was a little puzzled, as I did not do any specific changes on the Account controller, or any dependencies to the Account controller.
Unity works in a way, so when it instantiates a classs, Unity will use the default constructor with the most parameters. For the AccountController that meant that a constructor taking a UserManager<T> objects a s parameter, was called. To solve the problem, we need to tell Unity to use a constructor taking no parameters. This is done by adding:
container.RegisterType<AccountController>(new InjectionConstructor());to the method in which you register your types in the Unity container (typicaly in a file named UnityConfig.cs or similar)