如何解决“无法加载文件或程序集’Microsoft.Practices.Prism’”错误?

过去几天我一直在寻找使用Prism在WPF应用程序中修复错误,但没有任何运气,并且我认为Stackoverflow是否有任何想法。 请注意,对于Prism来说,我绝对是一个菜鸟:)

我正在学习如何使用Prism创建应用程序,Unity是我的dependency injection容器。 我正在尝试使用修改后的App.config文件加载模块,但是我收到了与引导程序相关的运行时错误。 错误消息将比我能提供的任何解释更准确,因此我已经包含App.config文件,出现错误的App.xaml.cs文件以及下面的错误信息。

提前感谢您提供的任何见解,如果您可能需要任何其他信息,请告诉我们!

编辑 :错误中引用的此文件目录(C:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ WpfApplication1.exe.Config第4行)引用App.config文件的第四行(部分名称) = …)。 我发现的所有教程和参考资料都与我所拥有的相同,但这仍然是引起问题的线条。

编辑2 :第一次编辑中引用的行实际上与官方Microsoft Prism指南中的行完全相同。

编辑3 :所以我从来没有弄清楚造成这种情况的根源,但是在我的沮丧中,我重新启动了我正在完成的示例项目,并没有在新项目中得到错误。 仍然不确定问题的根源是什么。

编辑4 :开个玩笑,同样的错误又回到了新程序中。


App.config中:

   

App.xaml.cs(错误出现在“bootstrapper.Run()”行中):

 using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace EmailClient { ///  /// Interaction logic for App.xaml ///  public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MyBootstrapper bootstrapper = new MyBootstrapper(); bootstrapper.Run(); } } } 

错误信息:

System.Configuration.ConfigurationErrorsException未处理HResult = -2146232062 Message =创建模块的配置节处理程序时出错:无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。 该系统找不到指定的文件。 (C:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ WpfApplication1.exe.Config line 4)Source = System.Configuration BareMessage =为模块创建配置节处理程序时出错:无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。 该系统找不到指定的文件。 Filename = C:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ WpfApplication1.exe.Config Line = 4 StackTrace:在System.Configuration的System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey,Boolean&isRootDeclaredHere)中.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject),位于System.Configuration.ClientConfigurationSystem.System.Configuration.Internal的System.Configuration.BaseConfigurationRecord.GetSection(String configKey)位于Microsoft的Microsoft.Practices.Prism.Modularity.ConfigurationStore.RetrieveModuleConfigurationSection()的Microsoft.Practices.Modularity.ConfigurationModuleCatalog.EnsureModulesDiscovered()的System.Configuration.ConfigurationManager.GetSection(String sectionName)上的.IinternalConfigSystem.GetSection(String sectionName) .Practices.Prism.Modularity.Con Microsoft的Microsoft.Practices.Prism.Moduleity.ModuleManager.Run()中的Microsoft.Practices.Prism.Modularity.ModuleCatalog.Initialize()的Microsoft.Practices.Prism.Modularity.ModuleCatalog.Load()中的figurationModuleCatalog.InnerLoad()。在Microsoft.Practices.Prism.Bootstrapper.Run()的Microsoft.Practices.Prism.UnityExtensions.UnityBootstrapper.Run(Boolean runWithDefaultConfiguration)中的Practices.UnityExtensions.UnityBootstrapper.InitializeModules()在EmailClient.App.OnStartup(StartupEventArgs e)中c:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ App.xaml.cs:System.Windows.Application。中的第21行。 b__1(Object unused)在System.Windows.Threading.ExceptionWrapper.InternalRealCall(在System.Windows.Threading.DispatcherOperation.InvokeImpl()的MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)上委托回调,Object args,Int32 numArgs)。 Windows.Threadi System.Threading.ExecutionContext.Run上的System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)中的ng.DispatcherOperation.InvokeInSecurityContext(Object state)(ExecutionContext executionContext,ContextCallback回调,Object state,Boolean preserveSyncCtx )System.Whows上的System.Windows.Threading.Dispatcher.ProcessQueue()中的System.Windows.Threading.DispatcherOperation.Invoke()处的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)。 Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)位于MS.Win32.HwndSubclass的MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)。在MS.Internal.Threadin的System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)中的DispatcherCallbackOperation(Object o) 在MS的System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Int32 numArgs)中的g.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)。 System.Windows.Threading上System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)的MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)中的Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) System.Windows.Application的System.Windows.Application.RunInternal(窗口窗口)中的System.Windows.Application.RunDispatcher(Object ignore)上的System.Windows.Threading.Dispatcher.Run()处的.Dispatcher.PushFrame(DispatcherFrame框架) .Run(窗口窗口)位于C:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ obj \ Debug \ App.g.cs中EmailClient.App.Main()的System.Windows.Application.Run()处:第50行InnerException:System.IO.FileNotFoundException HResult = -2147024894 Message =无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。 该系统找不到指定的文件。 Source = System.Configuration FileName = Microsoft.Practices.Prism FusionLog ====预绑定状态信息===日志:DisplayName = Microsoft.Practices.Prism(部分)警告:为程序集提供了部分绑定信息:WRN:程序集名称:Microsoft.Practices.Prism | 域ID:1 WRN:仅提供部分程序集显示名称时发生部分绑定。 警告:这可能导致活页夹加载错误的程序集。 警告:建议为程序集提供完全指定的文本标识,WRN:由简单名称,版本,区域性和公钥标记组成。 警告:有关此问题的更多信息和常见解决方案,请参阅白皮书http://go.microsoft.com/fwlink/?LinkId=109270 。 日志:Appbase = file:/// C:/ Users / Roger / Documents / TestPrismProject / WpfApplication1 / WpfApplication1 / bin / Debug / LOG:Initial PrivatePath = NULL调用程序集:System.Configuration,Version = 4.0.0.0,Culture = neutral ,PublicKeyToken = b03f5f7f11d50a3a。 日志:此绑定在默认加载上下文中启动。 日志:使用应用程序配置文件:C:\ Users \ Roger \ Documents \ TestPrismProject \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ WpfApplication1.exe.Config日志:使用主机配置文件:日志:使用C:\ Windows \中的计算机配置文件Microsoft.NET \框架\ v4.0.30319 \ CONFIG \ machine.config中。 日志:此时策略未应用于引用(私有,自定义,部分或基于位置的程序集绑定)。 日志:尝试下载新的URL文件:/// C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism.DLL。 日志:尝试下载新的URL文件:/// C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism/Microsoft.Practices.Prism.DLL。 日志:尝试下载新的URL文件:/// C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism.EXE。 日志:尝试下载新的URL文件:/// C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism/Microsoft.Practices.Prism.EXE。

  StackTrace: at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord) at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) InnerException: 

解决方案最终变得简单,因为这些事情经常是……我没有将我的App.config文件的Build Action更改为“Resource”(可以通过单击App.config文件并查看属性部分找到) 。

我有相同的例外(Prism 5.0),我的解决方案是将配置部分的类型设置为Microsoft.Practices.Prism.Composition,而不是我在许多示例中找到的Microsoft.Practices.Prism。

Microsoft.Practices.ServiceLocation参考添加到测试项目以解决此问题。

在较新版本的Prism(我使用的是6.3.0)中, ModulesConfigurationSection类位于Prism.Wpf.dll

将配置更改为:

  

StackTrace:在System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey,Boolean&isRootDeclaredHere)中的System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject)。 System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)中的Configuration.BaseConfigurationRecord.GetSection(String configKey)位于Microsoft.Practices.Prism.Modularity的System.Configuration.ConfigurationManager.GetSection(String sectionName)。 ConfigurationStore.RetrieveModuleConfigurationSection()at

调用堆栈本身表明Prism已经加载,但是当它尝试解析.config文件时, RetrieveModuleConfigurationSection发生exception。

你可以从那里开始检查可能是什么原因。

我将Prism.Core版本6.2.1更改为6.2.0,它适用于我。

我上次遇到过类似的问题。 我的解决方案不同且非常具体,但错误是相同的。 如果您要发送二进制文件并且目标PC使用Windows 10,请确保取消阻止所有文件。