GetExportedObject<IMyInterface>("PartName") Fails

Apr 26, 2009 at 8:07 PM
Edited Apr 26, 2009 at 8:10 PM
I have some code that successfully loads a Part when using System.ComponentModel.Composition.dll version 2009.1.23.0, size 220kb, but fails with an exception (ImportCardinalityMismatchException) when using version 2009.4.6.0 size 208kb.  The code below shows two Parts that implement the same interface, but have exported extra Contract Names to differentiate them.  The loader attempts to load the first Part by referencing both Exports (IMyInterface and "PartName"):

// Part definition
[Export(IMyInterface)]
[Export("PartName")]
public class MyFirstClass : IMyInterface
...

// Part 2 definition
[Export(IMyInterface)]
[Export("MyOtherPartName")]
public class MySecondClass: IMyInterface
...

// Part loader
CompositionContainer c = new CompositionContainer(new DirectoryCatalog("."));
c.Compose(new CompositionBatch());
IMyInterface p = c.GetExportedObject<IMyInterface>("PartName");  // <--- generates exception

The exception detail:
System.ComponentModel.Composition.ImportCardinalityMismatchException was unhandled
  Message="No exports were found that match the constraint '((exportDefinition.ContractName = \"PartName\") && (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") && \"MyInterface.IMyInterface\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))'."
  Source="System.ComponentModel.Composition"

Can anyone help me get this code working with the 2009.4.6.0 version?
Apr 27, 2009 at 5:16 PM

Hi,

In the latest drop MEF requires correspondence between types when matching named contracts. In effect this means that if you specify a contract name, you need to specify the exported type as well:

[Export("PartName", typeof(IMyInterface))]

Glenn posted some details here: http://blogs.msdn.com/gblock/archive/2009/04/12/mef-preview-5-changes-and-enhancements.aspx

Hope this helps!

Nick

From: indianajones [mailto:notifications@codeplex.com]
Sent: Sunday, April 26, 2009 12:08 PM
To: Nicholas Blumhardt
Subject: GetExportedObject<IMyInterface>("PartName") Fails [MEF:54524]

From: indianajones

I have some code that successfully loads a Part when using System.ComponentModel.Composition.dll version 2009.1.23.0, size 220kb, but fails with an exception (ImportCardinalityMismatchException) when using version 2009.4.6.0 size 208kb. The code:

// Part definition
[Export(IMyInterface)]
[Export("PartName")]
public class MyFirstClass : IMyInterface
...

// Part 2 definition
[Export(IMyInterface)]
[Export("MyOtherPartName")]
public class MySecondClass: IMyInterface
...

// Part loader
CompositionContainer c = new CompositionContainer(new DirectoryCatalog("."));
c.Compose(new CompositionBatch());
IMyInterface p = c.GetExportedObject<IMyInterface>("PartName"); // <--- generates exception

The exception detail:
System.ComponentModel.Composition.ImportCardinalityMismatchException was unhandled
Message="No exports were found that match the constraint '((exportDefinition.ContractName = \"PartName\") && (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") && \"MyInterface.IMyInterface\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))'."
Source="System.ComponentModel.Composition"

Can anyone help me get this code working with the 2009.4.6.0 version?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Apr 27, 2009 at 8:03 PM
Nick,

That was exactly what I needed.  Thanks very much.