如何在WPF中重新编译空的DirectoryCatalog for MEF

我有这个步骤的应用程序:

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

我有空目录’模块’后下载DLL我试图加载模块但没有成功。 我在线上有一个错误,我称之为DirectoryCatalog.Refresh()

用户代码未处理System.ComponentModel.Composition.ChangeRejectedException消息=组合保持不变。 由于以下错误,更改被拒绝:组合产生多个组合错误,有14个根本原因。 根本原因如下。 查看CompositionException.Errors属性以获取更多详细信息。

1)由不可重组的导入阻止的导出变化’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer..ctor(Parameter =“downloadedPartCatalogs”,ContractName =“Microsoft.Practices.Prism.MefExtensions.Modularity.DownloadedPartCatalogCollection”)’部分’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer’。

2)部分’Microsoft.Practices上的非重组导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader(ContractName =“Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader”)’阻止了导出的变化。 Prism.MefExtensions.Modularity.MefModuleManager”。

3)由不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor(Parameter =“moduleInitializer”,ContractName =“Microsoft.Practices.Prism.Modularity.IModuleInitializer”)’阻止出口的变化’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager’。

4)部分’Microsoft.Practices’中不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader(ContractName =“Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader”)’阻止了导出的变化。 Prism.MefExtensions.Modularity.MefModuleManager”。

5)由不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor(Parameter =“moduleInitializer”,ContractName =“Microsoft.Practices.Prism.Modularity.IModuleInitializer”)’阻止出口的变化’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager’。

6)部分’GXBilling.ViewsModels.ShellViewModel’上的不可重组导入’GXBilling.ViewsModels.ShellViewModel.ModuleManager(ContractName =“Microsoft.Practices.Prism.Modularity.IModuleManager”)’阻止了导出的变化。

7)发现多个导出匹配约束’((exportDefinition.ContractName ==“Microsoft.Practices.Prism.Regions.RegionAdapterMappings”)AndAlso(exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)AndAlso“Microsoft.Practices。 Prism.Regions.RegionAdapterMappings” .Equals(exportDefinition.Metadata.get_Item( “ExportTypeIdentity”))))”。

导致:无法设置导入’Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior..ctor(Parameter =“regionAdapterMappings”,ContractName =“Microsoft.Practices.Prism.Regions.RegionAdapterMappings”)”’部分’微软。 Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior”。 元素: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)由不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Regions.MefSelectorRegionAdapter..ctor(Parameter =“regionBehaviorFactory”,ContractName =“Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory”)阻止出口的变化’Microsoft.Practices.Prism.MefExtensions.Regions.MefSelectorRegionAdapter’。

9)由不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Regions.MefItemsControlRegionAdapter..ctor(Parameter =“regionBehaviorFactory”,ContractName =“Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory”)阻止出口的变化’Microsoft.Practices.Prism.MefExtensions.Regions.MefItemsControlRegionAdapter’。

10)由非重组导入’Microsoft.Practices.Prism.MefExtensions.Regions.MefContentControlRegionAdapter..ctor(Parameter =“regionBehaviorFactory”,ContractName =“Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory”)阻止的出口变化’Microsoft.Practices.Prism.MefExtensions.Regions.MefContentControlRegionAdapter’。

11)发现多个导出匹配约束’((exportDefinition.ContractName ==“Microsoft.Practices.Prism.Regions.IRegionViewRegistry”)AndAlso(exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)AndAlso“Microsoft.Practices。 Prism.Regions.IRegionViewRegistry” .Equals(exportDefinition.Metadata.get_Item( “ExportTypeIdentity”))))”。

导致:无法设置导入’Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior..ctor(Parameter =“regionViewRegistry”,ContractName =“Microsoft.Practices.Prism.Regions.IRegionViewRegistry”)”部分’微软。 Practices.Prism.MefExtensions.Regions.Behaviors.MefAutoPopulateRegionBehavior”。 元素: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)由不可重组的导入阻止的出口变化’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer..ctor(Parameter =“downloadedPartCatalogs”,ContractName =“Microsoft.Practices.Prism.MefExtensions.Modularity.DownloadedPartCatalogCollection”)’部分’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleInitializer’。

13)部分’Microsoft.Practices上的非重组导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager.mefFileModuleTypeLoader(ContractName =“Microsoft.Practices.Prism.MefExtensions.Modularity.MefFileModuleTypeLoader”)’阻止了导出的变化。 Prism.MefExtensions.Modularity.MefModuleManager”。

14)由不可重组的导入’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor(Parameter =“moduleInitializer”,ContractName =“Microsoft.Practices.Prism.Modularity.IModuleInitializer”)’阻止出口的变化’Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager’。

Source = System.ComponentModel.Composition StackTrace:位于System.ComponentModel.Composition.Holting.CatalogExportProvider的System.ComponentModel.Composition.Hosting.ExportProvider.OnExportsChanging(ExportsChangeEventArgs e)中的System.ComponentModel.Composition.CompositionResult.ThrowOnErrors(AtomicComposition atomicComposition)。 OnCatalogChanging(Object sender,ComposablePartCatalogChangeEventArgs e)位于System.ComponentModel.Composition的System.ComponentModel.Composition.Hosting.AggregateCatalog.OnChanging(ComposablePartCatalogChangeEventArgs e)的System.ComponentModel.Composition.Hosting.ComposablePartCatalogCollection.OnChanging(Object sender,ComposablePartCatalogChangeEventArgs e)。在System.ComponentModel.Composition.Hosting.DirectoryCatalog.Refresh的System.ComponentModel.Composition.Hosting.DirectoryCatalog.OnChanging(ComposablePartCatalogChangeEventArgs e)中的Hosting.ComposablePartCatalogCollection.OnContainedCatalogChanging(Object sender,ComposablePartCatalogChangeEventArgs e) ()在E:\ Projects中的GXBilling.Bootstrapper.RefreshCatalog()!New!\ GXBilling \ GXBilling \ Bootstrapper.cs:E:\ Projects中GXBilling.ViewsModels.WinLogOnViewModel.FWCore_OnDownloadedModulesCompleted(Object sender,EventArgs e)的第36行!新的!\ GXBilling \ GXBilling \ ViewsModels \ WinLogOnViewModel.cs:BillingCore中的BillingCore.FWCore.FWCore_ModuleDownloadedCompleted(对象发送者,ModuleDownloadedEventArgs e)中的第168行:E!\ Projects!New!\ GXBilling \ BillingCore \ FWCore.cs:BillingCore中的第169行。 E:\ Projects中的FWCore.client_DownloadFileCompleted(Object sender,DownloadFileCompletedEventArgs e)!在E:\ Projects中的BillingCore.BillingServiceRef.FileTransferServiceClient.OnDownloadFileCompleted(对象状态)中的新!\ GXBilling \ BillingCore \ FWCore.cs:第207行!新! GXBilling \ BillingCore \ Service References \ BillingServiceRef \ Reference.cs:System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)中的第739行,位于MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源) ,System.Whows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)中的System.Windows.Threading.DispatcherOperation.InvokeImpl()处于System.Threading.ExecutionContext.runTryCode(Object userData)的,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler) )System.Threading.ExecutionContext.Run上的System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode,backoutCode,Object userData)处于System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,Object状态)(ExecutionContext executionContext) System.Windows.Threading.Dispatcher.ProcessQueue上的System.Windows.Threading.DispatcherOperation.Invoke()处的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态),ContextCallback回调,对象状态,布尔值ignoreSyncCtx) ()at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam, 在System.Windows.Threading.ExceptionWrapper的MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)上的MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)中的IntPtr lParam,Boolean&handling) System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority)上的MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)中的.InternalRealCall(Delegate callback,Object args,Int32 numArgs)系统上MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)的MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)上的优先级,TimeSpan超时,Delegate方法,Object args,Int32 numArgs。系统中System.Windows.Window.Show()的System.Windows.Window.ShowHelper(Object booleanBox)上的System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)中的Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)。视窗 .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会触发重组 。 MEF部件上的导入只有在明确标记为这样的情况下才能重新组合,例如:

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

如果导入受重构影响,但未标记为AllowRecomposition=true ,则会出现错误:“由非重组导入阻止导出的更改”。

此外,通过构造函数导入根本不支持重构。 但是, 你可以解决这个问题 。

每当出现错误时:

“尝试获取类型实例时发生激活错误,密钥”“”

然后,请检查我们将要在Region注入的中是否指定了[Export]属性。

这是小事但可以浪费你几个小时的调试!

谢谢! 巴斯