Error using DependencyResolver to create a controller instance using MVC 3

I am using MVC 3 and using the following code when running the application ...

UnityContainer container = new UnityContainer(); new UnityMappings(container); DependencyResolver.SetResolver(new UnityServiceLocator(container)); 

Now when the application starts, I get the following error (but only sometimes) ...

Activation error while trying to get an instance of type IControllerFactory, key ""

Interestingly, if I continue the web request, the website is working fine.

Any ideas? I do not see what I am doing differently than before, when it worked fine.

Greetings, Jan.

+4
source share
2 answers

MVC3 requests a lot more than just the controllers from DependencyResolver . For most of them, MVC3 reverts to the default implementation if DependencyResolver does not return an instance.

In your case, it requests an IControllerFactory , which is unknown to your IoC container, and it throws an exception that gets into the UnityServiceLocator implementation, and returns null. Then MVC returns to the factory default controller.

Unlike other containers, IoC Unity does not provide the optional TryResolve and therefore does not support the correct, exclusive DependencyResolver implementation.

+6
source

I would suggest that you first look at the configuration and make sure everything is correct there, then I would make sure that I had all the assemblies necessary for Unity mentioned in the project. This error message may (in my experience) indicate a configuration error or missing DLL, perhaps the assembly you want to load in the container or another DLL needed for the DLL loaded by Unity?

Hope this helps.

0
source

Source: https://habr.com/ru/post/1381478/


All Articles