views:

312

answers:

2

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.

+1  A: 

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.

Daniel Plaisted
A: 

I you take a look at the Compose method in the SoapBox Core Host, you can see it using a DirectoryCatalog to find all parts in the directory. However, this isn't compiled against .NET 4, just against the preview release of MEF:

    private bool Compose()
    {
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog("."));

        _container = new CompositionContainer(catalog);

        try
        {
            _container.ComposeParts(this);
        }
        catch (CompositionException compositionException)
        {
            MessageBox.Show(compositionException.ToString());
            return false;
        }
        return true;
    }
Scott Whitlock