Dependency injection MEF:导入不';如果将组合代码放在构造函数中,则不起作用,但如果在返回之前将其放在函数中,则起作用
我有下面的服务Dependency injection MEF:导入不';如果将组合代码放在构造函数中,则不起作用,但如果在返回之前将其放在函数中,则起作用,dependency-injection,mef,Dependency Injection,Mef,我有下面的服务 [Export(typeof(IPluginsLoaderService))] internal class PluginsLoaderService : IPluginsLoaderService { public PluginsLoaderService() { AggregateCatalog catalog = new AggregateCatalog(); catalog.Catalogs.Add(new Director
[Export(typeof(IPluginsLoaderService))]
internal class PluginsLoaderService : IPluginsLoaderService
{
public PluginsLoaderService()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
[ImportMany(typeof (IBehaviorPlugin))]
public IEnumerable<IBehaviorPlugin> BehaviorPlugins { get; set; }
[ImportMany(typeof(IGeometryPlugin))]
public IEnumerable<IGeometryPlugin> ShapePlugins { get; private set; }
}
PluginsLoaderService不为null,但BehaviorPlugins和ShapePlugins属性包含0个元素
如果将compose parts代码放入如下函数,则加载插件
[Export(typeof(IPluginsLoaderService))]
internal class PluginsLoaderService : IPluginsLoaderService
{
[ImportMany(typeof (IBehaviorPlugin))]
public IEnumerable<IBehaviorPlugin> BehaviorPlugins { get; set; }
[ImportMany(typeof(IGeometryPlugin))]
public IEnumerable<IGeometryPlugin> ShapePlugins { get; private set; }
public IEnumerable<IBehaviorPlugin> GetBehaviorPlugins()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
return BehaviorPlugins;
}
public IEnumerable<IGeometryPlugin> GetShapePlugins()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
return ShapePlugins;
}
}
[导出(类型(IPluginsLoaderService))]
内部类PluginsLoaderService:IPluginsLoaderService
{
[进口数量(类型(IBehaviorPlugin))]
公共IEnumerable BehaviorPlugins{get;set;}
[进口数量(类型(IGeometryPlugin))]
公共IEnumerable ShapePlugins{get;private set;}
公共IEnumerable GetBehaviorPlugins()
{
AggregateCatalog catalog=新的AggregateCatalog();
catalog.Catalogs.Add(新目录目录(@.\..\Plugins\Shapes));
catalog.Catalogs.Add(新目录目录(@.\..\Plugins\behaviors));
CompositionContainer=新的CompositionContainer(目录);
容器。组件(本);
回归行为学;
}
公共IEnumerable GetShapePlugins()
{
AggregateCatalog catalog=新的AggregateCatalog();
catalog.Catalogs.Add(新目录目录(@.\..\Plugins\Shapes));
catalog.Catalogs.Add(新目录目录(@.\..\Plugins\behaviors));
CompositionContainer=新的CompositionContainer(目录);
容器。组件(本);
返回ShapePlugins;
}
}
为什么会这样?如何以更好的方式组织它?可以在MefBootstrapper内部执行吗?这是我的引导程序代码
public class Bootstrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window) this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolbarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolboxModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CanvasModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(IPluginsLoaderService).Assembly));
}
}
公共类引导程序:MefBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回容器.GetExportedValue();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
Application.Current.MainWindow=(窗口)this.Shell;
Application.Current.MainWindow.Show();
}
受保护的覆盖无效配置AggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(GetType().Assembly));
这个.AggregateCatalog.Catalogs.Add(新目录目录(@.\..\Plugins\Shapes));
this.AggregateCatalog.Catalogs.Add(新目录目录(@.\..\Plugins\behaviors));
this.AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(ToolbarModule.Assembly));
this.AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(ToolboxModule.Assembly));
this.AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(CanvasModule.Assembly));
this.AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(StatusBarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(新的组装目录(typeof(IPluginsLoaderService.Assembly));
}
}
为什么在一个属于MEF
部分的类中有作文?更好的做法是在应用程序启动时一次性组合所有部分,并将用于组合的逻辑放在一个bootstrapper类中,该类不是MEF
可组合的部分。当我试图通过bootstrapper对其进行配置时,它不知怎的不起作用。我将添加引导程序的代码。请你看一下,说一下那里少了什么好吗?
public class Bootstrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window) this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolbarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolboxModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CanvasModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(IPluginsLoaderService).Assembly));
}
}