简体   繁体   English

MEF DirectoryCatalog多次读取同一dll

[英]MEF DirectoryCatalog reads the same dll many times

I had a simple implementation of MEF loading some dlls (plugins) from a directory. 我有一个简单的MEF实现,可以从目录加载一些dll(插件)。 This was running well under MEF1 but now I want to use the same functionality with MEF2 and it gives me an IEnumerable that contains the right count of dlls that are in the directory but all the assemblies the same. 它在MEF1下运行良好,但现在我想对MEF2使用相同的功能,它为我提供了IEnumerable,该IEnumerable包含目录中正确数量的dll,但所有程序集都相同。

For example I have two assemblies: fakeplugin1.dll and fakeplugin2.dll in the directory. 例如,我在目录中有两个程序集:fakeplugin1.dll和fakeplugin2.dll。 They exports FakePlugin1 and FakePlugin2 classes. 他们导出FakePlugin1和FakePlugin2类。 Now when I call container.ComposeParts() I don't have anything in the list decorated with ImportMany and container.Catalog contains two assemblies in the directory but both of them are FakePlugin1. 现在,当我调用container.ComposeParts()时,列表中没有用ImportMany和container.Catalog装饰的东西。目录中包含两个程序集,但它们都是FakePlugin1。

Here's the code: 这是代码:

[ImportMany(typeof (IDCPlugin))]
        IEnumerable<IDCPlugin> workplaceControllers;
var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);
var agcatalogue = new AggregateCatalog(catalog);
var container = new CompositionContainer(agcatalogue);
container.ComposeParts();

I am trying to use ExportFactory and RegistrationBuilder but I've just realized that even the base functionality donesn't work as expected. 我正在尝试使用ExportFactory和RegistrationBuilder,但我刚刚意识到,即使基本功能也无法按预期工作。

What am I doing wrong? 我究竟做错了什么? Has something changed in MEF2 I should know? 我应该知道MEF2有什么变化吗? How to load the two different assemblies? 如何加载两个不同的程序集? :) :)

Thanks for your help in advance! 谢谢您的帮助!

Edit: It always creates two instances of the first type in the folder (ascending in abc). 编辑: 它总是在文件夹中创建第一个类型的两个实例(在abc中升序)。 If I put an other one in the folder it creates three of the same, etc. 如果我将另一个放在文件夹中,则会创建三个相同的文件夹,依此类推。

Edit: I have uploaded code to pastebin that gives the same result with MEF2: http://pastebin.com/3fWcujPS 编辑:我已将代码上载到pastebin,与MEF2给出相同的结果: http : //pastebin.com/3fWcujPS

A catalog will contain Import and Export definitions for anything detected. 目录将包含检测到的任何内容的导入和导出定义。 Regardless of if you actually need it. 无论您是否实际需要它。

This is a 'feature' of MEF. 这是MEF的“功能”。 You will need to either ImportMany and selectively filter the plugins you require. 您将需要ImportMany并有选择地过滤所需的插件。

So how do you handle multiple plugins gracefully? 那么,如何优雅地处理多个插件? Try this: 尝试这个:

[Export]
public class PluginService
{
   public const string BEST_PLUGIN = "BestPlugin";

   [ImportMany]
   public IEnumerable<Plugin> Plugins{ private get; set; }

   [Export(BEST_PLUGIN)]
   public Plugin BestPlugin{ get { return GetBestPlugin(); } }

   Plugin GetBestPlugin()
   {
       return Plugins.FirstOrDefault(); //or some other logic for selection
   }
}

If your plugins are resource intensive, you may want to consider Lazy initialization. 如果您的插件占用大量资源,则可能需要考虑延迟初始化。

Lazy<T, TMetadata> is a type provided by MEF to hold indirect references to exports. Lazy<T, TMetadata>是MEF提供的一种类型,用于保存对导出的间接引用。 Here, in addition to the exported object itself, you also get export metadata, or information that describes the exported object. 在这里,除了导出对象本身之外,您还可以获得导出元数据或描述导出对象的信息。 Each Lazy<T, TMetadata> contains an IOperation object, representing an actual operation, and an IOperationData object, representing its metadata. 每个Lazy<T, TMetadata>包含IOperation对象,表示一个实际的操作,并且一个IOperationData对象,表示它的元数据。

http://msdn.microsoft.com/en-us/library/dd460648.aspx#further_imports_and_importmany http://msdn.microsoft.com/en-us/library/dd460648.aspx#further_imports_and_importmany

MEF has strong rules on component cardinality (number of things ) to ensure that there are never any surprises but this does mean you have to be careful with your deployment. MEF在组件基数( 事物数)方面有严格的规则,以确保不会出现任何意外情况,但这确实意味着您必须谨慎部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM