我有一個Winforms桌面應用程序正在加載具有相同接口類型的多個MEF零件。無法加載多個MEF零件
問題: 當我嘗試加載相同的類型,我得到以下異常的不止一個:
的組成保持不變。由於以下錯誤,這些更改被拒絕:組合錯誤。根源在下面提供。查看CompositionException.Errors屬性以獲取更多詳細信息。
1)沒有有效的出口找到匹配約束「((exportDefinition.ContractName = 「BOCA.TaskPilot.Common.Extensions.IFolderViewExtension」)& &(exportDefinition.Metadata.ContainsKey( 「ExportTypeIdentity」)& &「說BOCA.TaskPilot.Common.Extensions.IFolderViewExtension「.Equals(exportDefinition.Metadata.get_Item(」ExportTypeIdentity「))))',無效的導出可能已被拒絕。
導致:無法在部件'TaskPilot.Windows.MainForm'上設置導入'TaskPilot.Windows.MainForm.FolderViewExtension(ContractName =「BOCA.TaskPilot.Common.Extensions.IFolderViewExtension」)'「。 元素:TaskPilot.Windows.MainForm.FolderViewExtension(ContractName = 「BOCA.TaskPilot.Common.Extensions.IFolderViewExtension」) - > TaskPilot.Windows.MainForm
這裏是加載部分的代碼:
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);
下面是在類的代碼對於每個MEF部分組成:
[Export(typeof(IFolderItemsViewExtension))
public partial class TaskTreeView : DevExpress.XtraEditors.XtraUserControl, IFolderItemsViewExtension, IPartImportsSatisfiedNotification]
這裏的導入主窗體上使用:
[ImportMany(AllowRecomposition = true)]
private IEnumerable<IFolderItemsViewExtension> TaskViewExtensions = null;
如果我取消註釋最後一個Catalog.Catalogs.Add行,則會引發異常。如果我運行它沒有它運行得很好。該行加載了實現IFolderItemsViewExtension接口的不同用戶控件。我試圖加載一個虛擬項目,它擁有的是用戶控件和那個接口,但我仍然得到相同的異常。無論我做什麼,我仍然會得到這個例外。
看來,只要我沒有加載多個相同類型的MEF部件導出,一切都運行良好。
這是使用來自MEF下載的System.ComponentModel.Composistion的最新版本2009.22.10.0。
丹尼爾大概就在這裏;不要忘記,除了反覆試驗之外,還有其他方法可以調試MEF問題。您應該可以轉儲組成狀態,如http://mef.codeplex.com/wikipage?title=Debugging%20and%20Diagnostics&referringTitle=Guide –
感謝您的幫助。我開始從自身出口和一切正常的控件中刪除Imports。我把Imports放回去了,現在我無法重現原來的問題。儘管......底線......現在起作用。謝謝丹尼爾和尼古拉斯。我現在正忙於MEF,因爲它在桌面上具有完全可擴展的應用程序的強大功能和潛力。 – user120985
非常感謝...解決了我的問題 –