Unable to load multiple MEF parts

I have a Winforms desktop application that loads several parts of MEF with the same type of interface.

Problem: When I try to load more than one type, I get the following exception:

The composition remains unchanged. Changes were rejected due to the following error (s): the composition produced a single compositional error. The main reason is given below. See the CompositionException.Errors property for more information.

1) No reliable export was found that matches the constraint '((exportDefinition.ContractName = "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") && & (exportDefinition.Metadata.ContainsKey ("ExportTypeIdentity") && "BOCA.TaskPilot. Common.Extensions.IFolderViewExtension ".Equals (exportDefinition.Metadata.get_Item (" ExportTypeIdentity ")))) ', an invalid export may be rejected.

Result: it is not possible to install the import of TaskPilot.Windows.MainForm.FolderViewExtension (ContractName = "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") 'on the part' TaskPilot.Windows.MainForm '. Element: TaskPilot.Windows.MainForm.FolderViewExtension (ContractName = "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") → TaskPilot.Windows.MainForm

Here is the code to download the parts:

AggregateCatalog catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); //string myExecName = Assembly.GetExecutingAssembly().Location; //string myPath = Path.GetDirectoryName(myExecName); catalog.Catalogs.Add(new DirectoryCatalog(@"C:\Data\TaskPilot\Development\Source\BOCA.TaskPilot.FolderView\bin\Debug")); catalog.Catalogs.Add(new DirectoryCatalog(@"C:\Data\TaskPilot\Development\Source\BOCA.TaskPilot.TaskView\bin\Debug")); // Uncomment below line and it works without exceptions raised //catalog.Catalogs.Add(new DirectoryCatalog(@"C:\Data\TaskPilot\Development\Source\BOCA.FileManager\bin\Debug")); var container = new CompositionContainer(catalog); container.ComposeParts(this); 

Here is the code in the class for each of the parts of the MEF:

 [Export(typeof(IFolderItemsViewExtension)) public partial class TaskTreeView : DevExpress.XtraEditors.XtraUserControl, IFolderItemsViewExtension, IPartImportsSatisfiedNotification] 

Here, the Import is used in the Main form:

 [ImportMany(AllowRecomposition = true)] private IEnumerable<IFolderItemsViewExtension> TaskViewExtensions = null; 

If I uncomment the last line of Catalog.Catalogs.Add, it throws an exception. If I run it without this, everything will be fine. This line loads another user control that implements the IFolderItemsViewExtension interface. I tried just loading a dummy project that all it has is a user control and this interface, and I still get the same exception. No matter what I do, I still get this exception.

Everything seems to be working fine until I load more than one export of the same type of MEF.

The latest version is 2009.22.10.0 System.ComponentModel.Composistion from the MEF download.

+7
c # winforms mef
source share
2 answers

The error indicates that it cannot find an export of type IFolderViewExtension. Note that this is different from importing the displayed IFolderItemsViewExtension.

I suppose the problem is not that you have multiple IFolderItemsViewExtensions, but you have multiple IFolderViewExtensions or another contract in which you have more than one of them that you use with an import that requires exactly one.

This may be because you have the same assembly in multiple directory directories. This is easy to do if you have a reference to the assembly and the local copy is set to true.

+4
source share

There may be more than one export statement in your export class. I ran into the same problem and this was resolved when I removed all other expert opinions from this export class. and now it works fine.

0
source share

All Articles