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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29537911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:04:06  来源:igfitidea点击:

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

c#wpfprism

提问by Edvard-D

I've been searching the past few days trying to get an error fixed in a WPF application using Prism, but haven't had any luck and thought I'd see if Stackoverflow had any ideas. Please note, I'm definitely a noob when it comes to Prism in general :)

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

I'm in the process of learning how to create an application using Prism, with Unity being my dependency injection container of choice. I'm trying to load modules using a modified App.config file, but I'm getting a runtime error related to the bootstrapper. The error messages will be more accurate than any explanation I can provide, so I've included the App.config file, App.xaml.cs file where the error is appearing, and the error information below.

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

Thank you in advance for any insight you can provide, and let me know if there's any additional information you might need!

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

EDIT: This file directory referenced in the error (C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config line 4) refers to the fourth line of App.config file (section name= ...). All the tutorials and references I've found are the same as what I have, but this is still the line that's causing issues it seems.

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

EDIT 2: The line referenced in the first edit is actually exactly the same as what's on the official Microsoft Prism guide.

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

EDIT 3: So I never did figure out the root of what was causing this, but in my frustration I restarted the example project I was working through and am not getting the error in the new project. Still not sure what the root of the problem was though.

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

EDIT 4: Just kidding, the same error is back in the new program.

编辑 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 (error appears on the "bootstrapper.Run()" line):

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();
        }
    }
}


Error Message:

错误信息:

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=109270for 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.

System.Configuration.ConfigurationErrorsException 未处理 HResult=-2146232062 消息=创建模块的配置节处理程序时出错:无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。该系统找不到指定的文件。(C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config line 4) Source=System.Configuration BareMessage=创建模块的配置部分处理程序时出错:无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。该系统找不到指定的文件。文件名=C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config Line=4 StackTrace:System.IO.FileNotFoundException HResult=-2147024894 Message=无法加载文件或程序集“Microsoft.Practices.Prism”或其依赖项之一。该系统找不到指定的文件。Source=System.Configuration FileName=Microsoft.Practices.Prism FusionLog==== 预绑定状态信息 === LOG:DisplayName = Microsoft.Practices.Prism(部分) WRN:为程序集提供了部分绑定信息:WRN:程序集名称:Microsoft.Practices.Prism | 域 ID:1 警告:仅提供程序集显示名称的一部分时会发生部分绑定。警告:这可能会导致活页夹加载错误的程序集。警告:建议为程序集提供完全指定的文本标识,警告:由简单名称、版本、文化、和公钥令牌。警告:请参阅白皮书http://go.microsoft.com/fwlink/?LinkId=109270有关此问题的更多信息和常见解决方案。LOG: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。LOG:此绑定在默认加载上下文中启动。LOG:使用应用程序配置文件:C:\Users\Roger\Documents\TestPrismProject\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe.Config LOG:使用主机配置文件:LOG:使用来自 C:\Windows\ 的机器配置文件Microsoft.NET\Framework\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。/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。/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: 

回答by Edvard-D

The solution ended up being simple, as these things often are... I hadn't changed my App.config file's Build Action to "Resource" (can be found by clicking on the App.config file and looking at the properties section).

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

回答by xander

I had the same exception (Prism 5.0) and my solution was to set the type of the config section to Microsoft.Practices.Prism.Composition instead of Microsoft.Practices.Prism which I found in many examples.

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

回答by Prasanjit

Add reference Microsoft.Practices.ServiceLocationto your Test Project to resolve this issue.

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

回答by Matt Williams

In the newer versions of Prism (I'm using 6.3.0), the ModulesConfigurationSectionclass is in Prism.Wpf.dll.

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

Change your configuration to this:

将您的配置更改为:

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

回答by Kamil Nowak

I've encountered similar issue last time. My solution was different and very specific but error was same. If you are sending you binaries and target PC uses Windows 10 make sure that you unblock all files.

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

回答by Lex Li

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

StackTrace:在 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& System.resultRuntime) 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() 在

The call stack itself indicates that Prism is already loaded, but when it tries to parse the .config file, an exception happens in RetrieveModuleConfigurationSection.

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

You might start from there to check what might be the cause.

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

回答by tetralobita

I changed Prism.Core version 6.2.1 to 6.2.0 and it works for me.

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