2011-09-08 72 views
0

我有一個MEF不導出插件的問題,顯然只有在安裝在c:\ Program Files(x86)中時纔會導出。MEF無法識別程序文件(x86)中的導出

我正在爲Windows服務實現一個非常簡單的MEF插件系統。

出口的TestService.dll(插件)

[Export(typeof(IScheduledService))] 
public class Service: IScheduledService 
{ ... } 

進口

public class ScheduledServices: IEnumerable<IScheduledService> 
{ 
    [ImportMany(typeof(IScheduledService))] 
    private List<IScheduledService> _services { get; set; } 

    ... 
} 

組成

var catalog = new DirectoryCatalog(PluginDirectory); 
var container = new CompositionContainer(catalog); 
container.ComposeParts(pluginCollection); 

這在測試中正常工作,並通過Windows窗體等驅動。它使用「InstallUtil」作爲服務安裝時甚至可以工作。但是,當彙總到Windows安裝程序並安裝在C:\ Program Files(x86)中時,它不會接收導出;

System.ComponentModel.Composition Information: 6 : The ComposablePartDefinition 
'TestService.Service' was ignored because it contains no exports. 

我在想它必須與某種CAS /權限有關嗎?

任何幫助表示讚賞!

回答

1

它可能是你定義的地方PluginDirectory。如果您使用相對路徑,例如.\plugins,它很可能使用服務啓動路徑,而不是安裝應用程序的位置,但安裝了svchost.exe的位置爲C:\Windows\System32。當我在服務使用MEF和要使用相對路徑,我使用了類似於:(?服務組件也許)

private static readonly string CodeBase = typeof(MyService).Assembly.CodeBase; 

public DirectoryCatalog GetCatalog(string relativePath) 
{ 
    // Grab our codebase location as a Uri. 
    Uri codeBase = new Uri(CodeBase); 
    // Get the local path for the codebase. 
    string path = codeBase.LocalPath.Substring(0, codeBase.LocalPath.LastIndexOf('\\'); 

    // Get the combined path 
    path = Path.Combine(path, relativePath); 

    return new DirectoryCatalog(path); 
} 

MyService是在主部件中的類型。這確保我總是使用正確的應用程序路徑(它也支持陰影複製程序集)。

檢查如何定義您的PluginDirectory路徑。