I am using ASP.NET MVC 5 with Unity 3 and Unity bootstrapper for ASP.NET MVC .
In the UnityConfig.cs file, I registered the following type of the RegisterTypes method:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IProjectRepository, ProjectRepository>();
}
In the MVC controller, I solve like this:
UnityContainer container = new UnityContainer();
public ActionResult List()
{
var projectRepository = container.Resolve<IProjectRepository>();
return View(projectRepository.GetAll());
}
But I get the following exception:
System.InvalidOperationException : type IProjectRepository does not have an available constructor.
When I register a type inside the controller, it works fine, so somehow the unity forgets what was defined in UnityConfig.RegisterTypes().
I put a breakpoint in UnityConfig.RegisterTypes()and the method is called.