Search code examples
c#.netmef

MEF Composition ImportMany yield two versions of same class


I haven't been able to find an answer for my problem. I'm using MEF to find and create my classes that implement IPlugIn, but I end up with two versions of each PlugIn class. I have determined that the AggregateCatalog only contains one Assembly that contains only 1 type of each class as a Part, but I am still getting two instances of each as the end result. I'm probably just doing something stupid, but I haven't found it yet. I'd appreciate any help...


...

    [ImportMany(typeof(IPlugIn))]
     public System.Lazy>[] Plugins { get; set; }

...
    //aggregatecatalog only contains one dll containing plugin classes, 4 of them
    container = new CompositionContainer(aggregateCatalog,
                                         CompositionOptions.DisableSilentRejection 
                                         | CompositionOptions.IsThreadSafe);\

    container.SatisfyImportsOnce(this);

    ...
    public void StartAll()
    {
    //We have 8 PlugIns here?? How? 
    if (Plugins == null || Plugins.Count() == 0)
    {

       log.Warn("No PlugIns Available to Start!");
       return;
    }

        foreach (var plug in Plugins)
        {
            log.Info("Starting PlugIn: " + plug.Value.GetName());
            plug.Value.Start();
        }
    }

   ...

    [Export(typeof(IPlugIn))]
    public class MyPlugIn : BasePlugIn, IPlugIn

    ...

I'm debugging in Visual Studio. I clearly see only one Assembly loading. The CompositionContainer has only 1 of each. After SatisfyImportOnce in StartAll(), I have two instances of each IPlugIn classes. Is this something with the way I am using ImportMany? I am open to any ideas.


Solution

  • The solution turned out to be simple. I removed the [InheritedExport] from the IPlugIn interface completely and that created the correct number of plugins. If I removed the [Export... from each PlugIn subclass, I got no plugins at all. It was the combination of the two that created the multiple instances.