I’m using Unity with an Asp.net MVC 3 app. Here is some code running in Application_Start…
UnityContainer container = new UnityContainer();
new UnityMappings(container).RegisterTypes();
DependencyResolver.SetResolver(new UnityServiceLocator(container));
The controllers are registered with the UnityMappings instance like so…
IEnumerable<Type> controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof (IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
{
container.RegisterType(t);
}
When I request a page I get the following errors… (The last one is specific to the view being requested.)
Activation error occured while trying to get instance of type
IControllerFactory, key “”Activation error occured while trying to get instance of type
IControllerActivator, key “”Activation error occured while trying to get instance of type
IViewPageActivator, key “”Activation error occured while trying to get instance of type
ModelMetadataProvider, key “”
And then strangely, I can click through all the exceptions and the page works absolutely fine! All the other dependencies are resolved just fine.
Its not a Visual Studio issue because it does it in different instances from different machines. I’ve had to turn off breaking on all exceptions so that I can get anything done.
Any ideas?
This error seems to have gone away when I use this…
… where UnityDependencyResolver is defined as…
Before I was using ‘UnityServiceLocator’ which is defined in ‘Microsoft.Practices.Unity’ like this…
Is ‘UnityServiceLocator’ not supposed to be used in this way?