I’m using MVC 3 and using the following code when the application starts…
UnityContainer container = new UnityContainer();
new UnityMappings(container);
DependencyResolver.SetResolver(new UnityServiceLocator(container));
Now when the app runs I’m getting the following error (but only sometimes)…
Activation error occured while trying to get instance of type
IControllerFactory, key “”
Interestingly, if I continue with the web request, the website works normally.
Any ideas? I can’t see what I’m doing differently from before when this worked fine.
Cheers, Ian.
MVC3 requests a lot more than just controllers from the
DependencyResolver. For most of them MVC3 falls back to the default implementation in case the DependencyResolver does not return an instance.In your case it requests the
IControllerFactorywhich is unknown to your IoC container and it throws an exception which is caught by theUnityServiceLocatorimplementation and null is returned. MVC then falls back to the default controller factory.Unlike other IoC containers Unity does not provide an optional
TryResolveand therefore does not support a proper exceptionless implementation of theDependencyResolver.