1

I am trying to load an individual add-in using MEF. My scenario is as follows: when my application starts, it will search for a set of add-ins and load them into my app.

After this start-up phase, I would like the user to be able to load an add-in of their choosing. This is the bit I'm really struggling with.

My initial loading code is as follows:

    var compositionContainer = new CompositionContainer();

    var assemblyCatalog = new AssemblyCatalog(@"C:\MyPath\Experiment.dll");

    .. catalogs for other addins..
    var aggregateCatalog = new AggregateCatalog(assemblyCatalog);

    CompositionContainer container = new CompositionContainer(aggregateCatalog);
    container.ComposeParts(this);

    foreach (var addin in addins)
    {
        addin.OnLoad(this);
    }

I feel I should be able to add the new add-in to the catalog in the existing container, but I cannot find any way of adding it. The following does not work:

var assemblyCatalog = new AssemblyCatalog(filePath);
var catalog = this.compositionContainer.Catalog as AggregateCatalog;
catalog.Catalogs.Add(assemblyCatalog);
this.compositionContainer.ComposeParts(this);

How can I load the add-in that is in the dll at filePath. Furthermore, is there any way that I can call the .OnLoad method on that new add-in without calling it on the ones already loaded?

Chris Spicer
  • 2,144
  • 1
  • 13
  • 22

1 Answers1

1

It sounds to me like what you're looking for is already implemented in the Prism library. Specifically you should have a look at its modularity documentation and demo.

Prism can manage your add-ins (modules in Prism terms) and allows you to load them immediately or on demand. When a module is loaded, its Initialize method (which is implemented by every module via the IModule interface) is called, so there won't be any redundant initialization calls.

I should also mention that Prism supports MEF out of the box.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110