English 中文(简体)
Can t load multiple MEF parts
原标题:

I have a Winforms desktop application that is loading multiple MEF parts with the same Interface type.

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

The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint ((exportDefinition.ContractName = "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity")))) , invalid exports may have been rejected.

Resulting in: Cannot set import TaskPilot.Windows.MainForm.FolderViewExtension (ContractName="BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") on part TaskPilot.Windows.MainForm . Element: TaskPilot.Windows.MainForm.FolderViewExtension (ContractName="BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") --> TaskPilot.Windows.MainForm

Here is the code to load 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:DataTaskPilotDevelopmentSourceBOCA.TaskPilot.FolderViewinDebug"));
        catalog.Catalogs.Add(new DirectoryCatalog(@"C:DataTaskPilotDevelopmentSourceBOCA.TaskPilot.TaskViewinDebug"));
        // Uncomment below line and it works without exceptions raised
        //catalog.Catalogs.Add(new DirectoryCatalog(@"C:DataTaskPilotDevelopmentSourceBOCA.FileManagerinDebug"));

        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);

Here s the code at the class for each of the MEF parts:

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

Here s the Import used on the Main form:

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

If I uncomment the last Catalog.Catalogs.Add line it throws the exception. If I run it without that it runs just fine. That line loads a different user control that implements the IFolderItemsViewExtension Interface. I ve tried to just load a dummy project that all it has is the user control and that interface and I still get the same exception. No matter what I do I still get this exception.

It seems that everything runs fine as long as I m not loading more than one of the same type of MEF part export.

This is using the latest version of 2009.22.10.0 of the System.ComponentModel.Composistion from the MEF download.

最佳回答

The error indicates that it can t find an export of type IFolderViewExtension. Note that this is different from the import of IFolderItemsViewExtension you have shown.

My guess is that the problem is not that you have multiple IFolderItemsViewExtensions, but that you have multiple IFolderViewExtensions, or there is some other contract that you have more than one of that you are using with an import that requires exactly one.

This may be caused because you have the same assembly in more than one of your directory catalogs. It is easy for this to happen if you have a reference to an assembly and copy local is set to true.

问题回答

I guess might have more than one export statement in your Export class. I was facing the same issue and this solved when i removed all other expert statement from that export class. and now it is working fine.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签