Unity IoC Newbie - Constructor Injection fail!
- HiI'm just starting trying to work with the Unity IoC framework, but I'm struggling to get the container resolving properly when it has a parameter in the constructor.I was watching Dave Hayden's webcast and it seems that I don't need to register the class I want to construct.So I have a class which wraps unitypublic class UnityContainer : IContainer{private Microsoft.Practices.Unity.IUnityContainer _container;public UnityContainer(){_container = new Microsoft.Practices.Unity.UnityContainer().RegisterType<IChannel, SimpleEvent>().RegisterType<IChannelID, ChannelID>();//_container.Configure<InjectedMembers>().ConfigureInjectionFor<Broker>(new InjectionConstructor(new ChannelFactory));}public T Resolve<T>(){return (T) _container.Resolve<T>();}}I also have another class which I want to construct with unitypublic class Broker : IBroker{ChannelFactory _factory;public Broker(ChannelFactory factory){_factory = factory;}...public class ChannelFactory{private readonly IContainer _container;public ChannelFactory(IContainer container){_container = container;}...In my test I have[TestMethod]public void T5_Create_Broker_With_Unity(){IContainer container = new UnityContainer();IBroker broker = container.Resolve<Broker>();}When I run the test code it fails ... telling me "The parameter factory could not be resolved when attempting to call constructor ... Are you missing a type mapping?" I did try and register the type, but it didn't make any difference. (Full error below)I must be doing something pretty elementary wrong! I'd be grateful if someone could point out what I am missing.Many ThxSimonError textTest method Test_CommunicationsBroker.BrokerTests.T5_Create_Broker_With_Unity threw exception: Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "MessageRouter.Broker", name = "". Exception message is: The current build operation (build key Build Key[MessageRouter.Broker, null]) failed: The parameter factory could not be resolved when attempting to call constructor MessageRouter.Broker(MessageRouter.ChannelFactory factory). (Strategy type BuildPlanStrategy, index 3) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[MessageRouter.Broker, null]) failed: The parameter factory could not be resolved when attempting to call constructor MessageRouter.Broker(MessageRouter.ChannelFactory factory). (Strategy type BuildPlanStrategy, index 3) ---> System.InvalidOperationException: The parameter factory could not be resolved when attempting to call constructor MessageRouter.Broker(MessageRouter.ChannelFactory factory). ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[MessageRouter.ChannelFactory, null]) failed: The parameter container could not be resolved when attempting to call constructor MessageRouter.ChannelFactory(MessageRouter.IContainer container). (Strategy type BuildPlanStrategy, index 3) ---> System.InvalidOperationException: The parameter container could not be resolved when attempting to call constructor MessageRouter.ChannelFactory(MessageRouter.IContainer container). ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[MessageRouter.IContainer, null]) failed: The current type, MessageRouter.IContainer, is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3) ---> System.InvalidOperationException: The current type, MessageRouter.IContainer, is an interface and cannot be constructed. Are you missing a type mapping?.
すべての返信
- OK so I've sort of sorted it.Trying to get unity to construct thispublic class ChannelFactory{private readonly IContainer _container;public ChannelFactory(IContainer container){_container = container;}...was the problem. It wasn't able to resolve the IContainer for some reason (lack of understanding on my part rather than a failure of unity, btw).Is it good practice or perhaps even necessary for the container to be in a static class?ThxS

