9

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.

2 Answers 2

8

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.

4
  • I accepted this as the answer, as your clarification that this is expected behavior was exactly what I was looking for. Thank you, I appreciate it.
    – Silas
    Aug 30, 2013 at 20:41
  • I think this answer is not accurate. Actually you need to catch this exception and return null
    – Illidan
    Nov 23, 2014 at 9:30
  • I voted up this answer; however, I am working in an enterprise environment. Is there any documentation that you can point me to in order to support the answer?
    – Thomas
    Jun 7, 2016 at 18:58
  • The best I can do is point you at the source code - the current MVC code is at github at github.com/aspnet/Mvc, older versions are on codeplex at aspnetwebstack.codeplex.com. Jun 7, 2016 at 19:17
2

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>
6
  • 1
    On a different note, you said: " Unity will recursively try to resolve the parameter types for one of the constructors unless you explicitly tell it not to..." - How does one tell unity NOT to do this?
    – Silas
    Aug 29, 2013 at 19:26
  • 1
    Specify the constructor to use and the values to inject in the Unity config. Updated my answer.
    – Haney
    Aug 29, 2013 at 19:28
  • 1
    Thanks, I will look at that specifically as I am scrutinizing the code.
    – Silas
    Aug 29, 2013 at 19:29
  • 1
    David, thank you for your answer. It helped me to understand how Unity works better than I did before. I marked the other as the correct answer, however, as it did exactly answer what I was looking for.
    – Silas
    Aug 30, 2013 at 20:41
  • 1
    Just for reference in the future, if you get resolution exceptions, look closely at the error message in the exception. It includes a trace of where in the object graph it hit the problem, including parameter names. So you don't have wonder where in the code it was when it couldn't resolve, the error message will tell you. Sep 1, 2013 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.