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

1.6k views Asked by At

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?

2

There are 2 answers

0
Juergen On BEST ANSWER

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

0
Phil Sandler On

You don't show how (or whether) the unity container is shared across modules. Based on your variable name ("unityContainer"), I'm guessing it's a local variable within the module? That means you have two separate container instances, each of which would require registration.