.NET Framework Bookmark and Share   
 index > Managed Extensibility Framework > Unity IoC Newbie - Constructor Injection fail!
 

Unity IoC Newbie - Constructor Injection fail!

Hi

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.

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 unity

public 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 unity

public 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 Thx

Simon


Error text

Test 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?.
Simon Woods
OK so I've sort of sorted it.

Trying to get unity to construct this

public 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?

Thx

S

Simon Woods

You can use google to search for other answers

Custom Search

More Threads

• One framework to rule them all?
• Comparison to the AddIn libraries?
• pinvoke query
• MEF for server side applications
• Composite CompositionContainer or ValueResolvers?
• not able to call vs 2005 c++ "clr" jni dll in other machines without vs 2005???????
• Looking for example of some usercontrol/tabitem exporting a ToolBar object
• how to add or integrate download accelerator to IE
• MEF and singletons
• CollectionViewSource between addin-host