Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity registration errors on types that I am not resolving

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.

like image 583
Silas Avatar asked Aug 29 '13 19:08

Silas


2 Answers

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.

like image 153
Chris Tavares Avatar answered Oct 16 '22 07:10

Chris Tavares


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>
like image 40
Haney Avatar answered Oct 16 '22 05:10

Haney