2016-09-26 49 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 

}