在不同模块中解析时,Unity 无法解析类型
我目前正在开发带有 Unity 容器和 WCF 服务的 PRISM 应用程序。 在模块(使用 WCF 代理)中,我为 WCF 客户端注册一个 ChannelFactory,如下所示:
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();
现在,一切正常,因此我决定在另一个模块中使用此 ChannelFactory。 以下是该模块的 Initialize 方法:
var test = unityContainer.Resolve<DuplexChannelFactory<IGenericTradingInterface>>();
test.Open();
var test2 = test.CreateChannel();
test2.GetServerInformation();
test.Close();
因此,除了缺少注册之外,此代码与其他模块的代码完全相同。
运行此程序时,出现以下异常:
Exception is: InvalidOperationException - The type DuplexChannelFactory`1 has mu
ltiple constructors of length 3. Unable to disambiguate.
这似乎是 ChannelFactory 的解析和 Ctor 的问题,但为什么统一可以解析第一个模块中的工厂而不是这个模块中的工厂?
我也不明白这个异常消息,因为我想在注册中调用特定的Ctor:
new InjectionConstructor(
instanceContext,
"NetNamedPipeBinding_IGenericTradingInterface")
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有展示统一容器如何(或是否)在模块之间共享。根据您的变量名称(“unityContainer”),我猜测它是模块内的局部变量?这意味着您有两个独立的容器实例,每个容器实例都需要注册。
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.
事实证明,问题出在模块初始化的顺序上。第二个模块首先被调用,因此 Unity 采用参数最多的 CTor,而 DuplexChannelFactory 最多有 3 个,而且很多。谢谢,于尔根
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