2015-09-08 41 views
0

我有項目,像這樣的結構(每個箱子是分開組裝):MEF [導入]當進口對象有[導入]不工作,引用的程序集

Project structure

我在插件類Plugin1程序集:

[Export("WcfService")] 
public class Plugin1 : BaseService, IPlugin1 
{ 
    [Import] 
    private ILogger _logger; 

    [Import] 
    private IDatabaseContextFactory f; 
} 

ILogger及其實現在MyHost.Common程序集中。 IDatabaseContextFactory及其實現在數據庫程序集中。

MyHost.Host搜索標記爲WcfService的導出。

[Export(typeof(IHost))] 
internal class Host : IHost 
{ 
    private class MefContainerHelper 
    { 
     [ImportMany("WcfService")] 
     public IList<object> Services = new List<object>(); 
    } 

    public void LoadServicesFromCompositionContainer(CompositionContainer compositionContainer) 
    { 
     var helper = new MefContainerHelper(); 
     compositionContainer.SatisfyImportsOnce(helper); 
    } 
} 

當進口在這一行:

[Import] 
    private IDatabaseContextFactory f; 

被註釋掉了它的工作原理,否則MEF提出了異常:「沒有有效的出口發現」爲WcfService導入(不IDatabaseContextFactory),或產生空爲ImportMany收集。

雖然在兩種情況下調試CompositionContainer看起來都是一樣的。

編輯:

CompositionContainer中使用此代碼內置:

 var commonAssembly = new AssemblyCatalog(typeof(ILogger).Assembly); 
     var dirAssembly = new DirectoryCatalog("./Plugins/"); 
     var aggregateCatalog = new AggregateCatalog(commonAssembly, dirAssembly); 

     return new CompositionContainer(aggregateCatalog); 

DatabaseContextFactory和Plugin1在dirAssembly。

+0

您是否將MyHost.Common程序集添加到您的MEF容器中?您可以在安裝CompositionContainer時分享代碼嗎? – TomDoesCode

+0

我編輯了我的帖子 – smokeing

回答

0

我終於找到了解決方案!

數據庫中存在一個錯誤(導入不存在的服務)。

我想知道爲什麼例外是在插件上,而不是數據庫層。