c# - 如何在 WPF 中重新组合 MEF 的空 DirectoryCatalog

标签 c# wpf prism mef

我有一个包含以下步骤的应用程序:

  1. 启动应用程序和ConfigureAggregateCatalog
  2. 登录用户到应用
  3. 将 DLL MEF 模块下载到名为“Modules”的目录
  4. 刷新目录目录 -- 出现问题

下载 dll 后,我有空目录“模块”,我尝试加载模块但没有成功。我调用 DirectoryCatalog.Refresh() 时出现错误

System.ComponentModel.Composition.ChangeRejectedException was unhandled by user code Message=The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced multiple composition errors, with 14 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.

1) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer..ctor (Parameter="downloadedPartCatalogs", ContractName="Microsoft.Practices.Prism.MefExtensions.Modularity.DownloadedPartCatalogCollection")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer'.

2) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader (ContractName="Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

3) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor (Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

4) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader (ContractName="Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

5) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor (Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

6) Change in exports prevented by non-recomposable import 'GXBilling.ViewsModels.ShellViewModel.ModuleManager (ContractName="Microsoft.Practices.Prism.Modularity.IModuleManager")' on part 'GXBilling.ViewsModels.ShellViewModel'.

7) More than one export was found that matches the constraint '((exportDefinition.ContractName == "Microsoft.Practices.Prism.Regions.RegionAdapterMappings") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Microsoft.Practices.Prism.Regions.RegionAdapterMappings".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.

Resulting in: Cannot set import 'Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior..ctor (Parameter="regionAdapterMappings", ContractName="Microsoft.Practices.Prism.Regions.RegionAdapterMappings")' on part 'Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior'. Element: Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior..ctor (Parameter="regionAdapterMappings", ContractName="Microsoft.Practices.Prism.Regions.RegionAdapterMappings") --> Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior --> AssemblyCatalog (Assembly="Microsoft.Practices.Prism.MefExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

8) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Regions.MefSelectorRegionAdapter..ctor (Parameter="regionBehaviorFactory", ContractName="Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory")' on part 'Microsoft.Practices.Prism.MefExtensions.Regions.MefSelectorRegionAdapter'.

9) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Regions.MefItemsControlRegionAdapter..ctor (Parameter="regionBehaviorFactory", ContractName="Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory")' on part 'Microsoft.Practices.Prism.MefExtensions.Regions.MefItemsControlRegionAdapter'.

10) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Regions.MefContentControlRegionAdapter..ctor (Parameter="regionBehaviorFactory", ContractName="Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory")' on part 'Microsoft.Practices.Prism.MefExtensions.Regions.MefContentControlRegionAdapter'.

11) More than one export was found that matches the constraint '((exportDefinition.ContractName == "Microsoft.Practices.Prism.Regions.IRegionViewRegistry") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Microsoft.Practices.Prism.Regions.IRegionViewRegistry".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.

Resulting in: Cannot set import 'Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior..ctor (Parameter="regionViewRegistry", ContractName="Microsoft.Practices.Prism.Regions.IRegionViewRegistry")' on part 'Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior'. Element: Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior..ctor (Parameter="regionViewRegistry", ContractName="Microsoft.Practices.Prism.Regions.IRegionViewRegistry") --> Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior --> AssemblyCatalog (Assembly="Microsoft.Practices.Prism.MefExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

12) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer..ctor (Parameter="downloadedPartCatalogs", ContractName="Microsoft.Practices.Prism.MefExtensions.Modularity.DownloadedPartCatalogCollection")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer'.

13) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader (ContractName="Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

14) Change in exports prevented by non-recomposable import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor (Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' on part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.

Source=System.ComponentModel.Composition StackTrace: at System.ComponentModel.Composition.CompositionResult.ThrowOnErrors(AtomicComposition atomicComposition) at System.ComponentModel.Composition.Hosting.ExportProvider.OnExportsChanging(ExportsChangeEventArgs e) at System.ComponentModel.Composition.Hosting.CatalogExportProvider.OnCatalogChanging(Object sender, ComposablePartCatalogChangeEventArgs e) at System.ComponentModel.Composition.Hosting.ComposablePartCatalogCollection.OnChanging(Object sender, ComposablePartCatalogChangeEventArgs e) at System.ComponentModel.Composition.Hosting.AggregateCatalog.OnChanging(ComposablePartCatalogChangeEventArgs e) at System.ComponentModel.Composition.Hosting.ComposablePartCatalogCollection.OnContainedCatalogChanging(Object sender, ComposablePartCatalogChangeEventArgs e) at System.ComponentModel.Composition.Hosting.DirectoryCatalog.OnChanging(ComposablePartCatalogChangeEventArgs e) at System.ComponentModel.Composition.Hosting.DirectoryCatalog.Refresh() at GXBilling.Bootstrapper.RefreshCatalog() in E:\Projects!New!\GXBilling\GXBilling\Bootstrapper.cs:line 36 at GXBilling.ViewsModels.WinLogOnViewModel.FWCore_OnDownloadedModulesCompleted(Object sender, EventArgs e) in E:\Projects!New!\GXBilling\GXBilling\ViewsModels\WinLogOnViewModel.cs:line 168 at BillingCore.FWCore.FWCore_ModuleDownloadedCompleted(Object sender, ModuleDownloadedEventArgs e) in E:\Projects!New!\GXBilling\BillingCore\FWCore.cs:line 169 at BillingCore.FWCore.client_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e) in E:\Projects!New!\GXBilling\BillingCore\FWCore.cs:line 207 at BillingCore.BillingServiceRef.FileTransferServiceClient.OnDownloadFileCompleted(Object state) in E:\Projects!New!\GXBilling\BillingCore\Service References\BillingServiceRef\Reference.cs:line 739 at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Window.ShowHelper(Object booleanBox) at System.Windows.Window.Show() at System.Windows.Window.ShowDialog() InnerException:

有部分代码由属性 Import 标记:

[Import]
public ShellViewModel ViewModel

[Import(AllowRecomposition = false)]
public IModuleManager ModuleManager { get; set; }

[Import(typeof(IShell), AllowRecomposition = true)]
public IShell MainShell { get; set; }

[ImportingConstructor]
public ModuleImporting(IRegionManager regionManager)

仅此而已。我的代码中没有 IEnureable 或 IList 等。那么问题出在哪里呢?

最佳答案

刷新DirectoryCatalog会触发recomposition 。仅当明确标记为 MEF 部件上的导入时,才能对其进行重构,例如如下所示:

[ImportMany(typeof(IFoo),AllowRecomposition=true)]
IEnumerable<IFoo> Foos { private get; set; }

如果导入受到重组的影响,但未使用 AllowRecomposition=true 进行标记,则会收到错误消息:“导出中的更改被不可重组的导入阻止”。

此外,通过构造函数导入根本不支持重组。然而,you can work around this .

关于c# - 如何在 WPF 中重新组合 MEF 的空 DirectoryCatalog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224904/

相关文章:

c# - C# 和 C 之间的串行连接通信时遇到问题

c# - 来自 Explorer 的命令行参数在每个空格处拆分

c# - 横向扩展 Windows 服务

C# 委托(delegate)字典添加

C# 放弃在非静态上下文中使用 const

c# - WPF 页面的 NavigationService 为空。如何在我的页面上获取 NavigationService?

wpf - 使用 MVVM 在 WPF 中打印 TreeView

c# - Prism MVVM,我应该将 IRegionManager 放在 ViewModel 中还是其他地方?

c# - 如何从 Prism 应用程序的各个模块中的 Unity 容器中获取相同的对象实例?

c# - 从不同的 ViewModel 执行相同的 Prism 命令