如何解决 "Could not load file or assembly 'Microsoft.Practices.Prism' " 错误?

How to resolve "Could not load file or assembly 'Microsoft.Practices.Prism' " error?

过去几天我一直在搜索,试图使用 Prism 修复 WPF 应用程序中的错误,但运气不佳,我想看看 Whosebug 是否有任何想法。请注意,总的来说,我绝对是 Prism 的菜鸟:)

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

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

EDIT: 错误中引用的这个文件目录(C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config第4行)指的是[=49的第四行=] 文件(部分名称 = ...)。我找到的所有教程和参考资料都与我所拥有的相同,但这仍然是导致问题的原因。

编辑 2:第一次编辑中引用的行实际上与官方 Microsoft Prism guide.

中的行完全相同

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

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


App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>
  </configSections>

  <modules>
    <module assemblyFile="EmailModule.dll" moduleType="EmailService.ModuleDefinitions.Module, EmailService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="EmailModule" startupLoaded="true" />
  </modules>


  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

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
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MyBootstrapper bootstrapper = new MyBootstrapper();
            bootstrapper.Run();
        }
    }
}

错误信息:

System.Configuration.ConfigurationErrorsException was unhandled HResult=-2146232062 Message=An error occurred creating the configuration section handler for modules: Could not load file or assembly 'Microsoft.Practices.Prism' or one of its dependencies. The system cannot find the file specified. (C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config line 4) Source=System.Configuration BareMessage=An error occurred creating the configuration section handler for modules: Could not load file or assembly 'Microsoft.Practices.Prism' or one of its dependencies. The system cannot find the file specified. Filename=C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config Line=4 StackTrace: at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at Microsoft.Practices.Prism.Modularity.ConfigurationStore.RetrieveModuleConfigurationSection() at Microsoft.Practices.Prism.Modularity.ConfigurationModuleCatalog.EnsureModulesDiscovered() at Microsoft.Practices.Prism.Modularity.ConfigurationModuleCatalog.InnerLoad() at Microsoft.Practices.Prism.Modularity.ModuleCatalog.Load() at Microsoft.Practices.Prism.Modularity.ModuleCatalog.Initialize() at Microsoft.Practices.Prism.Modularity.ModuleManager.Run() at Microsoft.Practices.Prism.UnityExtensions.UnityBootstrapper.InitializeModules() at Microsoft.Practices.Prism.UnityExtensions.UnityBootstrapper.Run(Boolean runWithDefaultConfiguration) at Microsoft.Practices.Prism.Bootstrapper.Run() at EmailClient.App.OnStartup(StartupEventArgs e) in c:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\App.xaml.cs:line 21 at System.Windows.Application.<.ctor>b__1(Object unused) 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 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.LegacyInvokeImpl(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.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at EmailClient.App.Main() in c:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\obj\Debug\App.g.cs:line 50 InnerException: System.IO.FileNotFoundException HResult=-2147024894 Message=Could not load file or assembly 'Microsoft.Practices.Prism' or one of its dependencies. The system cannot find the file specified. Source=System.Configuration FileName=Microsoft.Practices.Prism FusionLog==== Pre-bind state information === LOG: DisplayName = Microsoft.Practices.Prism (Partial) WRN: Partial binding information was supplied for an assembly: WRN: Assembly Name: Microsoft.Practices.Prism | Domain ID: 1 WRN: A partial bind occurs when only part of the assembly display name is provided. WRN: This might result in the binder loading an incorrect assembly. WRN: It is recommended to provide a fully specified textual identity for the assembly, WRN: that consists of the simple name, version, culture, and public key token. WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue. LOG: Appbase = file:///C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/ LOG: Initial PrivatePath = NULL Calling assembly : System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism.DLL. LOG: Attempting download of new URL file:///C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism/Microsoft.Practices.Prism.DLL. LOG: Attempting download of new URL file:///C:/Users/Roger/Documents/TestPrismProject/WpfApplication1/WpfApplication1/bin/Debug/Microsoft.Practices.Prism.EXE. LOG: Attempting download of new URL file:///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: 

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

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

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

解决方案最终变得很简单,因为这些事情通常是...我没有将 App.config 文件的构建操作更改为 "Resource"(可以通过单击 App.config 文件并查看属性部分)。

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

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

将引用 Microsoft.Practices.ServiceLocation 添加到您的测试项目以解决此问题。

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

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

将您的配置更改为:

<configSections>
    <section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf"/> 
</configSections>