Unity IoC Newbie - Constructor Injection fail!Hi <div><br/></div> <div>I'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.</div> <div><br/></div> <div>I was watching Dave Hayden's webcast and it seems that I don't need to register the class I want to construct.</div> <div><br/></div> <div>So I have a class which wraps unity</div> <div><br/></div> <div> <div> <div>    public class UnityContainer : IContainer</div> <div>    {</div> <div>        private Microsoft.Practices.Unity.IUnityContainer _container;</div> <div><br/></div> <div>        public UnityContainer()</div> <div>        {</div> <div>            _container = new Microsoft.Practices.Unity.UnityContainer()</div> <div>                .RegisterType&lt;IChannel, SimpleEvent&gt;()</div> <div>                .RegisterType&lt;IChannelID, ChannelID&gt;();</div> <div><br/></div> <div>            //_container.Configure&lt;InjectedMembers&gt;().ConfigureInjectionFor&lt;Broker&gt;(new InjectionConstructor(new ChannelFactory));</div> <div><br/></div> <div>        }</div> <div><br/></div> <div>        public T Resolve&lt;T&gt;()</div> <div>        {</div> <div>            return (T) _container.Resolve&lt;T&gt;();</div> <div>        }</div> <div><br/></div> <div>    }</div> </div> <div><br/></div> <div>I also have another class which I want to construct with unity</div> <div><br/></div> <div> <div> <div>   public class Broker : IBroker</div> <div>    {</div> <div><br/></div> <div>        ChannelFactory _factory;</div> <div>        </div> <div>        public Broker(ChannelFactory factory)</div> <div>        {</div> <div>            _factory = factory;</div> <div>        }</div> </div> </div> <div> ...</div> <div><br/></div> <div> <div>    public class ChannelFactory</div> <div>    {</div> <div><br/></div> <div>        private readonly IContainer _container;</div> <div><br/></div> <div>        public ChannelFactory(IContainer container)</div> <div>        { </div> <div>            _container = container;</div> <div>        }<br/></div> </div> <div>...</div> <div><br/></div> <div>In my test I have</div> <div><br/></div> <div> <div> <div>        [TestMethod]</div> <div>        public void T5_Create_Broker_With_Unity()</div> <div>        { </div> <div>            IContainer container = new UnityContainer();</div> <div>            IBroker broker = container.Resolve&lt;Broker&gt;();</div> <div><br/></div> <div><br/></div> <div>        }</div> </div> <div><br/></div> <div><br/></div> <div>When I run the test code it fails ... telling me &quot;The parameter factory could not be resolved when attempting to call constructor ... Are you missing a type mapping?&quot; I did try and register the type, but it didn't make any difference. (Full error below)</div> <div><br/></div> <div>I must be doing something pretty elementary wrong! I'd be grateful if someone could point out what I am missing. </div> <div><br/></div> <div>Many Thx</div> <div><br/></div> <div>Simon</div> <div><br/></div> <div><br/></div> <div>Error text</div> <div><br/></div> <div>Test method Test_CommunicationsBroker.BrokerTests.T5_Create_Broker_With_Unity threw exception:  Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = &quot;MessageRouter.Broker&quot;, name = &quot;&quot;. 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) ---&gt;  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) ---&gt;  System.InvalidOperationException: The parameter factory could not be resolved when attempting to call constructor MessageRouter.Broker(MessageRouter.ChannelFactory factory). ---&gt;  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) ---&gt;  System.InvalidOperationException: The parameter container could not be resolved when attempting to call constructor MessageRouter.ChannelFactory(MessageRouter.IContainer container). ---&gt;  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) ---&gt;  System.InvalidOperationException: The current type, MessageRouter.IContainer, is an interface and cannot be constructed. Are you missing a type mapping?.<br/></div> </div> </div>© 2009 Microsoft Corporation. All rights reserved.Mon, 20 Apr 2009 06:01:13 Z1ab1e51d-7950-4742-9f9d-db229c8f8057http://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/1ab1e51d-7950-4742-9f9d-db229c8f8057#1ab1e51d-7950-4742-9f9d-db229c8f8057http://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/1ab1e51d-7950-4742-9f9d-db229c8f8057#1ab1e51d-7950-4742-9f9d-db229c8f8057Simon Woodshttp://social.msdn.microsoft.com/Profile/en-US/?user=Simon%20WoodsUnity IoC Newbie - Constructor Injection fail!Hi <div><br/></div> <div>I'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.</div> <div><br/></div> <div>I was watching Dave Hayden's webcast and it seems that I don't need to register the class I want to construct.</div> <div><br/></div> <div>So I have a class which wraps unity</div> <div><br/></div> <div> <div> <div>    public class UnityContainer : IContainer</div> <div>    {</div> <div>        private Microsoft.Practices.Unity.IUnityContainer _container;</div> <div><br/></div> <div>        public UnityContainer()</div> <div>        {</div> <div>            _container = new Microsoft.Practices.Unity.UnityContainer()</div> <div>                .RegisterType&lt;IChannel, SimpleEvent&gt;()</div> <div>                .RegisterType&lt;IChannelID, ChannelID&gt;();</div> <div><br/></div> <div>            //_container.Configure&lt;InjectedMembers&gt;().ConfigureInjectionFor&lt;Broker&gt;(new InjectionConstructor(new ChannelFactory));</div> <div><br/></div> <div>        }</div> <div><br/></div> <div>        public T Resolve&lt;T&gt;()</div> <div>        {</div> <div>            return (T) _container.Resolve&lt;T&gt;();</div> <div>        }</div> <div><br/></div> <div>    }</div> </div> <div><br/></div> <div>I also have another class which I want to construct with unity</div> <div><br/></div> <div> <div> <div>   public class Broker : IBroker</div> <div>    {</div> <div><br/></div> <div>        ChannelFactory _factory;</div> <div>        </div> <div>        public Broker(ChannelFactory factory)</div> <div>        {</div> <div>            _factory = factory;</div> <div>        }</div> </div> </div> <div> ...</div> <div><br/></div> <div> <div>    public class ChannelFactory</div> <div>    {</div> <div><br/></div> <div>        private readonly IContainer _container;</div> <div><br/></div> <div>        public ChannelFactory(IContainer container)</div> <div>        { </div> <div>            _container = container;</div> <div>        }<br/></div> </div> <div>...</div> <div><br/></div> <div>In my test I have</div> <div><br/></div> <div> <div> <div>        [TestMethod]</div> <div>        public void T5_Create_Broker_With_Unity()</div> <div>        { </div> <div>            IContainer container = new UnityContainer();</div> <div>            IBroker broker = container.Resolve&lt;Broker&gt;();</div> <div><br/></div> <div><br/></div> <div>        }</div> </div> <div><br/></div> <div><br/></div> <div>When I run the test code it fails ... telling me &quot;The parameter factory could not be resolved when attempting to call constructor ... Are you missing a type mapping?&quot; I did try and register the type, but it didn't make any difference. (Full error below)</div> <div><br/></div> <div>I must be doing something pretty elementary wrong! I'd be grateful if someone could point out what I am missing. </div> <div><br/></div> <div>Many Thx</div> <div><br/></div> <div>Simon</div> <div><br/></div> <div><br/></div> <div>Error text</div> <div><br/></div> <div>Test method Test_CommunicationsBroker.BrokerTests.T5_Create_Broker_With_Unity threw exception:  Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = &quot;MessageRouter.Broker&quot;, name = &quot;&quot;. 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) ---&gt;  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) ---&gt;  System.InvalidOperationException: The parameter factory could not be resolved when attempting to call constructor MessageRouter.Broker(MessageRouter.ChannelFactory factory). ---&gt;  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) ---&gt;  System.InvalidOperationException: The parameter container could not be resolved when attempting to call constructor MessageRouter.ChannelFactory(MessageRouter.IContainer container). ---&gt;  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) ---&gt;  System.InvalidOperationException: The current type, MessageRouter.IContainer, is an interface and cannot be constructed. Are you missing a type mapping?.<br/></div> </div> </div>Sat, 18 Apr 2009 09:50:05 Z2009-04-19T06:03:40Zhttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/1ab1e51d-7950-4742-9f9d-db229c8f8057#f6ea94a3-c7a2-48ee-9319-4364ca98ad0chttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/1ab1e51d-7950-4742-9f9d-db229c8f8057#f6ea94a3-c7a2-48ee-9319-4364ca98ad0cSimon Woodshttp://social.msdn.microsoft.com/Profile/en-US/?user=Simon%20WoodsUnity IoC Newbie - Constructor Injection fail!OK so I've sort of sorted it. <div><br/></div> <div>Trying to get unity to construct this</div> <div><br/></div> <div> <div> public class ChannelFactory</div> <div>    {</div> <div><br/></div> <div>        private readonly IContainer _container;</div> <div><br/></div> <div>        public ChannelFactory(IContainer container)</div> <div>        { </div> <div>            _container = container;</div> <div>        }</div> <div>...</div> <div><br/></div> <div>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).</div> <div><br/></div> <div>Is it good practice or perhaps even necessary for the  container to be in a static class?</div> <div><br/></div> <div>Thx</div> <div><br/></div> <div>S</div> <div><br/></div> </div>Mon, 20 Apr 2009 05:59:53 Z2009-04-20T06:01:10Z