Search code examples
c#unity-containerwcf-clientchannelfactoryduplex-channel

Unity can't resolve type, when resolving in a different module


I'm currently working on a PRISM app with the unity container and a WCF service. In the module(with the WCF proxy) I register a ChannelFactory for the WCF client as follows:

InstanceContext instanceContext = new InstanceContext(new TradingPlatformCallback());
unityContainer.RegisterType<DuplexChannelFactory<IGenericTradingInterface>, DuplexChannelFactory<IGenericTradingInterface>>(
    new ContainerControlledLifetimeManager(),
    new InjectionConstructor(
        instanceContext,
        "NetNamedPipeBinding_IGenericTradingInterface"));

DuplexChannelFactory<IGenericTradingInterface> factory = unityContainer.Resolve<DuplexChannelFactory<IGenericTradingInterface>>();

factory.Open();
IGenericTradingInterface test = factory.CreateChannel();
test.GetServerInformation();
factory.Close();

Now, everything is working fine, so I decided to use this ChannelFactory in another module. Here's the Initialize method of the module:

var test = unityContainer.Resolve<DuplexChannelFactory<IGenericTradingInterface>>();
test.Open();

var test2 = test.CreateChannel();
test2.GetServerInformation();
test.Close();

So this code is exactly the same as that of the other module, except the missing registration.

When running this, I get the following exception:

Exception is: InvalidOperationException - The type DuplexChannelFactory`1 has mu
ltiple constructors of length 3. Unable to disambiguate.

It seems to be a problem with the resolving and the Ctors of the ChannelFactory, but why can unity resolve the factory in the first module and not in this one?

I also do not understand this exception message, since I thought to have called a specific Ctor in the registration with:

new InjectionConstructor(
                instanceContext,
                "NetNamedPipeBinding_IGenericTradingInterface")

Any ideas?


Solution

  • It turns out that the problem was the order of module initialization. The second module was called first and so Unity takes the CTor with the most parameters and DuplexChannelFactory has at most 3 and many of them. Thanks, Juergen