2016-09-26 44 views
1

设置我的依赖注入后,我得到与基数不匹配的MEF错误。我相信我正在从接口正确导出,当我检查程序时,实际目录是空的。不知道我做错了什么!试图做与MEF的依赖注入和获取错误

集装箱组成

private static CompositionContainer CreateContainer() 
    { 
     AggregateCatalog catalog = new AggregateCatalog(); 
     catalog.Catalogs.Add(new AssemblyCatalog(typeof(IClient).Assembly)); 
     CompositionContainer container = new CompositionContainer(catalog); 
     return container;  
    } 

创建容器

static void Main(string[] args) 
    { 
     CompositionContainer container = CreateContainer(); 
     IClient contactList = container.GetExportedValue<IClient>(); 

     // More code below, but error is happening on the above line 
    } 

客户端导入

namespace MyWcfProgram.WcfProgram 
{ 
    [Export(typeof(IClient))] 
    public class Client : IClient 
    {  
     private readonly ILogic contactLogic; 

     [ImportingConstructor] 
     public Client([Import] ILogic contactLogic) 
     { 
      if(contactLogic == null) 
      { 
       throw new Exception(); 
      } else 
      { 
       this.contactLogic = contactLogic; 
      } 
     } 

     // MORE BELOW LEFT OUT 
    } 

逻辑进口

namespace MyWcfProgram.Logic 
{ 
    [Export(typeof(ILogic))] 
    public class Logic : ILogic 
    { 
     private readonly IData dataAccess; 

     [ImportingConstructor] 
     public Logic([Import] IData dataAccess) 
     { 
      if (dataAccess == null) 
      { 
       throw new Exception(); 
      } 

      this.dataAccess = dataAccess; 
     } 

Data.cs

namespace MyWcfProgram.Data 
{ 
    [Export(typeof(IData))] 
    public class Data : IData 
    { 
     private string connectionString = "CONNECTION STRING IS HERE"; 
     private System.Data.SqlClient.SqlConnection myconnection = new System.Data.SqlClient.SqlConnection(); 

     // More code below, but no constructor since there are no dependencies 

} 
+0

是否'ImportingConstructor'需要有一个构造函数的参数'Import'属性? – Fabio

+0

是的,已更新以包括逻辑以及.. dataAccess没有任何依赖项被导入 –

+0

无论如何增加了数据,以防万一有问题 –

回答

0

我知道你没有问这个,但我可能会建议使用SimpleInjector代替,如果你没有在MEF hard依赖。 MEF是陈旧的,如果你所需要的只是一个IOC框架,那么使用SimpleInjector代替生活将会变得更好。

集装箱组成

private static Container CreateContainer() 
    { 
     var container = new Container(); 

     container.Register<IClient, MyWcfProgram.WcfProgram.Client>(); 
     container.Register<ILogic, MyWcfProgram.Logic.Logic>(); 
     container.Register<IData, MyWcfProgram.Data.Data>(); 

     return container;  
    } 

创建容器

static void Main(string[] args) 
    { 
     var container = CreateContainer(); 
     IClient contactList = container.GetExportedValue<IClient>(); 

     // More code below, but error is happening on the above line 
    } 

客户端导入

namespace MyWcfProgram.WcfProgram 
{ 
    public class Client : IClient 
    {  
     private readonly ILogic contactLogic; 

     public Client(ILogic contactLogic) 
     { 
      if(contactLogic == null) 
      { 
       throw new Exception(); 
      } else 
      { 
       this.contactLogic = contactLogic; 
      } 
     } 

     // MORE BELOW LEFT OUT 
    } 

逻辑进口

namespace MyWcfProgram.Logic 
{ 
    public class Logic : ILogic 
    { 
     private readonly IData dataAccess; 

     public Logic(IData dataAccess) 
     { 
      if (dataAccess == null) 
      { 
       throw new Exception(); 
      } 

      this.dataAccess = dataAccess; 
     } 

Data.cs

namespace MyWcfProgram.Data 
{ 
    public class Data : IData 
    { 
     private string connectionString = "CONNECTION STRING IS HERE"; 
     private System.Data.SqlClient.SqlConnection myconnection = new System.Data.SqlClient.SqlConnection(); 

     // More code below, but no constructor since there are no dependencies 

}