0

我正在尝试注册相同接口的多个实现,如下所示:-

**DLL A: Module.cs**
_container.RegisterType<IFoo, Foo1>("Foo1");

**DLL B: Module.cs**
var childContainer = _container.CreateChildContainer(); //childcontainer
childContainer.RegisterType<IFoo, Foo2>("Foo2");

**DLL A: Resolve IFoo for Foo2** (But, resolving IFoo for Foo1 is fine)
var foo2 = container.Resolve<IFoo>("Foo2"); //Error

注意:我在这里收到的容器是“childcontainer”。已检查哈希码。

错误:

Microsoft.Practices.Unity.dll 中出现了“System.InvalidOperationException”类型的第一次机会异常

附加信息:当前类型 IFoo 是一个接口,无法构造。您是否缺少类型映射?


但是,如果我在 Foo1 之后在 DLL A 中注册 Foo2 ,它就可以工作,就像这样:

_container.RegisterType<IFoo, Foo1>("Foo1");
_container.RegisterType<IFoo, Foo2>("Foo2");

Registration (Register) and Resolution (Resolve)取决于范围和装配?我想要第一种工作方法。任何想法 ?

4

1 回答 1

0

我尝试了下一个示例并且正在为我工​​作。请注意程序集引用和命名空间包含。

**Dll A

Public  Interface Ifoo

 End Interface

**Dll B


 Public Class Class1

        Public Shared function registerOtherType(container AS IUnityContainer) As IUnityContainer
            Dim c As IUnityContainer = container.CreateChildContainer
            c.RegisterType(of Ifoo, foo2)("2")
            Return c
     End Function


    End Class

    Public Class foo2
        Implements Ifoo

    End Class

**Dll C

    Public Class foo
        Implements Ifoo

 End Class

    Sub Main()
        'init()
        'Dim manager As IClasificationManagement = ServiceLocator.Current.GetInstance(Of IClasificationManagement)()
        'manager.SwapDescrition("1", "2")
        'Console.WriteLine("Operacion Realizada")
        'Console.Read()
        Dim container As IUnityContainer = New UnityContainer()
        container.RegisterType(Of Ifoo, foo )("1")
        dim child As IUnityContainer = Class1.registerOtherType(container)
        child.Resolve(of Ifoo)("2")

    End Sub
于 2013-04-24T10:22:56.740 回答