Alexander Hunter
Alexander Hunter

Reputation: 89

MEF Composition .NET 4.0

Thanks in advance for your assistance. I have the following exported part:

[Export (typeof(INewComponent))]  // orignally tried just [Export} here and importing NewComponent below
public class NewComponent : INewComponent  
{  
    // does stuff including an import  
}

The Console test program imports the above:

public class Program   
{    

    [Import]  // have tried variations on importing "NewComponent NewComponent" etc  
    public INewComponent NewComponent
    {
        get;
        set;
    }

    public static void Main(string[] args)
    {
        var p = new Program();
        var catalog = new AssemblyCatalog(typeof(Program).Assembly);
        var container = new CompositionContainer(catalog);
        container.ComposeParts(p);
}

The Composition fails with these CompositionExceptions (I removed the namespace to protect the guilty :)):

1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "INewComponent") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "INewComponent".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

The Composition works successfully if I do the composition in the main program like this:

public class Program  
{      

    public static void Main(string[] args)
    {
        INewComponent newComponent = new NewComponent();

        var catalog = new AssemblyCatalog(typeof(Program).Assembly);
        var container = new CompositionContainer(catalog);
        container.ComposeParts(newComponent);
    }
}

Thank You

Upvotes: 1

Views: 393

Answers (2)

Wim Coenen
Wim Coenen

Reputation: 66723

In your NewComponent class you wrote this:

// does stuff including an import

If there is a problem with that unshown import, then MEF will complain about the Program.NewComponent import instead of the actual deeper cause. This is called "stable composition". Stable composition can be useful, but it also complicates the debugging of a failed composition.

You can follow the instructions in the MEF documentation about Diagnosing Composition Errors to home in on the actual cause.

In a small program, you can also try to call container.GetExportedValue<ISomeExport>() for a few exports until you find the one that is causing problems.

Upvotes: 2

Jim Counts
Jim Counts

Reputation: 12795

Is your Exported part contained in the same Assembly as Program? If it is in a separate DLL, you need to include that Assembly in your catalog as well, like this:

var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NewComponent).Assembly));
var container = new CompositionContainer(aggregateCatalog);
// etc...

If that's doesn't work, then there is a nice open source tool called Visual MEFx that can help you analyze your catalog. Here is a short article about setting it up:

Getting Started With Visual MEFx

Upvotes: 3

Related Questions