【问题标题】:Unity can't resolve type, when resolving in a different module在不同的模块中解析时,Unity 无法解析类型
【发布时间】:2011-10-22 00:31:25
【问题描述】:

我目前正在开发一个带有统一容器和 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的resolving和Ctors有问题,但是为什么unity可以在第一个模块中解析工厂而不是在这个模块中呢?

我也不明白这个异常消息,因为我认为在注册时调用了特定的 Ctor:

new InjectionConstructor(
                instanceContext,
                "NetNamedPipeBinding_IGenericTradingInterface")

有什么想法吗?

【问题讨论】:

    标签: c# unity-container wcf-client channelfactory duplex-channel


    【解决方案1】:

    您没有展示统一容器如何(或是否)跨模块共享。根据您的变量名称(“unityContainer”),我猜它是模块内的局部变量?这意味着您有两个独立的容器实例,每个都需要注册。

    【讨论】:

      【解决方案2】:

      原来问题出在模块初始化的顺序上。第二个模块首先被调用,因此 Unity 采用具有最多参数的 CTor,而 DuplexChannelFactory 最多有 3 个,其中很多。谢谢,于尔根

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-11
        • 2018-05-31
        • 2021-10-16
        • 1970-01-01
        • 2015-10-22
        相关资源
        最近更新 更多