Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instruct MEF to use any available assemblies

I am trying out the Managed Extensibility Framework for the first time in Visual Studio 2010 beta 2 using the System.ComponentModel.Composition from .net-4.0.

I have been unable to get the CompositionContainer to find my implementation assemblies using the two alternative routines below.

First attempt (this worked in an older codeplex release of MEF):

var composition = new CompositionBatch();
composition.AddPart(this);
var container = new CompositionContainer(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
container.Compose(composition);

Second attempt (this worked in beta 1, I think):

var aggregateCatalog = new AggregateCatalog(
    new AssemblyCatalog(Assembly.GetExecutingAssembly()),
    new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
var compositionContainer = new CompositionContainer(aggregateCatalog);
compositionContainer.ComposeParts(this);

Is there a new way to do this in beta 2?

EDIT: It turned out to be nothing to do with the composition. I had a static property representing my imported implementation:

[Import] public static ILog Log { get; set; }

which should have been:

[Import] public ILog Log { get; set; }

I marked Daniel's answer as accepted because the sage advice of debugging in a more thorough fashion solved the problem.

like image 483
grenade Avatar asked Nov 14 '22 14:11

grenade


1 Answers

What is failing? Is there an import you expect to be satisfied which is not being satisfied? Are you calling GetExports() and it is failing?

You can break in the debugger after the catalog has been created, and mouse over the aggregateCatalog variable to inspect it and see what parts are in it. My guess is that the parts are probably in the catalog, and the problem is somewhere else in your code. A likely cause is that you have a collection import which is using the [Import] attribute instead of [ImportMany], and/or that your parts are being rejected because they have imports that can't be satisfied.

like image 85
Daniel Plaisted Avatar answered Dec 25 '22 04:12

Daniel Plaisted