by Rob
17. February 2010 00:27
One of the complaints I hear regularly is that debugging container resolution exceptions can be difficult. Well, unknown to many Unity has a handy extension method that helps you with this very problem. GetRootException can be found in the Microsoft.Practices.Composite.ExceptionExtensions class. The method loops through inner exceptions to find the likely place that the most relevant exception took place. Below you can see three constructors for classes that are composed together. All should resolve except that the ApplicationCommands class takes an interface IInterfaceWeForgetToAdd that doesn’t have an implementation added to the container.
public Shell(IShellViewModel shellViewModel)
public ShellViewModel(IApplicationCommands commands, IEnumerable<IMenuItemViewModel> menuItems)
public ApplicationCommands(IRegionManager regionManager, IInterfaceWeForgetToAdd iwfta)
Below is a screen shot of where you’d need to dig around to find the relevant exception message that tells you what the problem is when resolving the Shell.

Or alternatively you can call the GetRootException extension method on the caught exception and then look at the message to get the following useful pointer to where things went wrong:
"The current type, Contracts.Services.IInterfaceWeForget, is an interface and cannot be constructed. Are you missing a type mapping?"
1e27d39f-9012-44d1-bd67-7b5ec01c4bce|0|.0
Tags: unity