When we implement the Unity Framework for the first time, we may get this error.
The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be constructed. Are you missing a type mapping?
When we implement the IDependencyResolver of Unity Framework, GetService method will resolve the requested service type and return the actual object. The moment we write our custom resolver by implementing the Unity Framework, MVC framework things that we are going to take care of resolving all the types.
Type resolving happens in the following order .
- - IControllerFactory
- - IControllerActivator
- - HomeController
We resolve for the objects which we create in our application and not for the default objects of MVC. so we need to return null for the objects for which we did not register. That way we can avoid the error because the MVC by default have resolvers. When we return null in our custom resolver, it considers the default resolver and proceed with implementation.
public object GetService(Type serviceType)
{
return this._container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : null;
}
Please refer this link to get more insight on this.