I have an MVC4 application that is using Unity for dependency resolution. One of the things we have is a logger decorator for the Unity container - for the sole purpose of logging when any dependency resolution fails.
I am finding that there are a significant number of types that are failing to resolve that my app is NOT explicitly resolving. These types are:
IControllerFactory
IControllerActivator
IViewPageActivator
ModelMetadataProvider
ITempDataProvider
IActionInvoker
IAsyncActionInvoker
The exceptions thrown are all like this:
The type ITempDataProvider
does not have an accessible constructor.
...with their own type.
These exceptions only occur on application startup, and for now we are logging them and continuing. Thus the application runs just fine even after the errors occur.
I hate swallowing errors without a good reason, and without understanding what is attempting to resolve these in the first place, I have no good reason.
Questions:
1) Does anyone know who/what is attempting to resolve these? It is not anywhere in my codebase. 2) If somewhere down in the framework these are attempting to be resolved, is it expected that my app will provide resolution in Unity for these? 3) Or is this just expected behavior, and I should swallow these exceptions?
I understand that this is not a lot to go on, but I am hoping that someone else has seen these types of errors and will be able to point me in the right direction.
You've hooked up Unity as the DependencyResolver in MVC, correct? All those types you're seeing are internally used by the MVC framework itself, and it (MVC) is what's trying to resolve them.
The MVC code under the hood catches the error if something doesn't resolve and falls back to the standard implementation. It's done this way so that there's a uniform way to plug in custom implementations of those things if you want them.
You should not have to do anything with these exceptions - just let them flow back out to the caller, and MVC will do the right thing.
If you resolve a class through Unity that doesn't have a parameterless constructor, Unity will recursively try to resolve the parameter types for one of the constructors unless you explicitly tell it not to... If that fails you'll get errors such as the ones you're seeing for types you may not have directly resolved. So it is likely that you're resolving YOUR class which has a constructor that takes an MVC class which itself has constructor parameters such as ModelMetaDataProvider
, etc.
Example of specifying constructor:
<register type="IMyThing" mapTo="MyThing">
<constructor>
<param name="x" type="MyType1" />
<param name="y" type="MyType2" />
</constructor>
</register>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With