我正在构建一个实现MEF的Windows服务的过程。这个想法是,该服务应该管理子组件并且是可插入的,其中可以在运行时引入任意数量的新组件,而不必经历全面的服务重新编译和部署场景。这些组件应该是可插拔的,这意味着只要我将一个输出组件放到光盘上的指定文件夹中,Windows服务就应该说“嘿,这是一个运行的新组件”,并且只是做这件事。这是MEF的力量。非常酷的东西到目前为止。Microsoft扩展性框架 - MEF CompositionContainer
我理解创建AggregateCatalog和CompositionContainer以导入所有可用的子组件(组件)的概念。这一切都很好,并且在Windows服务启动时它按预期工作。但是,我最终会在运行时引入新组件,所以在某些时候,我需要使用CompositionContainer来识别添加的任何新输出程序集。
我对以下内容的关注是对性能的任何影响。将CompositionContainer逻辑放入如下所示的循环中是否安全/高效:
[ImportMany(AllowRecomposition = true)]
private IEnumerable<Lazy<IMySubComponentTask, IDictionary<string, object>>>
MySubComponentTasks { get; set; }
while (true)
{
//Create a general aggregate catalog
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MySubComponentTaskFactory).Assembly));
//Create a new directory catalog
var directoryCatalog = new DirectoryCatalog(@".\Extensions");
//Add the DLLs in the directory to the aggregate catalog
catalog.Catalogs.Add(directoryCatalog);
//Create the current composition container to create the parts
var container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
任何反馈或输入被赞赏。
谢谢, 迈克
谢谢,我可能要走这条路。这是另一件事,我没有在上面简单提及。对于CompositionContainer在每次定时查找期间发现的每个子组件,我将把子组件作业踢到一个单独的线程中。为了管理新线程的数量,我将不得不检查是否有一个线程已经运行在上一次定时查找期间发现的子组件作业。你使用线程的经验是多少,线程多少?在某些时候,我可能有15个以上的子组件用于这件事 – 2011-01-14 15:36:58