ResolutionFailedException using RegisterType and Resolve

Jul 14, 2010 at 2:22 PM
Edited Jul 14, 2010 at 3:28 PM

I have two different implementations of System.Windows.Input.ICommand and would like to associate each type with a name. So I created the below code;

private void foo()

{

   IUnityContainer container = new UnityContainer();

   container.RegisterType<ICommand, MyCommand1>("TBD1");

   container.RegisterType<ICommand, MyCommand2>("TBD2");

   ICommand commmand = container.Resolve<ICommand>("TBD1");

}

On resolve I get the following exception;

Microsoft.Practices.Unity.ResolutionFailedException was caught

Message="Resolution of the dependency failed, type = \"System.Windows.Input.ICommand\", name = \"TBD1\".

Exception occurred while: while resolving. Exception is: InvalidOperationException - The type MyCommand1 cannot be constructed. You must configure the container to supply this value.

----------------------------------------------- At the time of the exception, the container was:

Resolving Program.Component.foo.MyCommand1,TBD1 (mapped from System.Windows.Input.ICommand, TBD1)\"

Source="Microsoft.Practices.Unity" NameRequested="TBD1"

TypeRequested="ICommand"

StackTrace: at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)

at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)

at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, String name, ResolverOverride[] overrides)

at Program.Component.foo()

at Program.Component.Main(string[] args)

InnerException: System.InvalidOperationException

Message="The type MyCommand1 cannot be constructed. You must configure the container to supply this value."

Source="Microsoft.Practices.Unity"

StackTrace: at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor)

at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)

at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)

at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)

at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) InnerException:

 

What am I doing wrong?

 

Update: The two implementations of ICommand does not contain any variables at the moment and there is only one default constructor that takes no parameters. Also tried to add the [InjectionConstructor] attribute on both constructor but had no effect.

Jul 14, 2010 at 4:04 PM

Found the problem. For some reason the class was defined as public but the constructor was internal. By setting both to public it works fine now.