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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29698667/
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:05:30  来源:igfitidea点击:

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

c#wpfdllprism

提问by Edvard-D

I've run into an error with a test WPF application that I'm using to learn Prism. The error log is below, but the gist of it is that it can't seem to find Microsoft.Practices.Unity version 3.5.1.0. From my understanding this can mean two things: either it can't find any version of Unity, or it's finding the wrong version number of Unity.

我在用于学习 Prism 的测试 WPF 应用程序中遇到错误。错误日志如下,但其要点是它似乎无法找到 Microsoft.Practices.Unity 3.5.1.0 版。根据我的理解,这可能意味着两件事:要么找不到任何版本的 Unity,要么找到错误的 Unity 版本号。

To rule out the first option (at least I believe this rules it out) I double checked that the dll is installed in the projects that need it and in the bin folder; there is only one copy of Unity within each project and the bin folder. It is installed, but per the properties description it says that the version is 3.5.0.0. Per the Object Browser it says the dll is in a folder titled "Unity.3.5.1404.0". Note that the dll was installed using NuGet, and the two groups of packages I installed are called "Prism" and "Prism.UnityExtensions." It's the second one that the Unity dll is in.

为了排除第一个选项(至少我相信这排除了它),我仔细检查了 dll 是否安装在需要它的项目和 bin 文件夹中;每个项目和 bin 文件夹中只有一个 Unity 副本。它已安装,但根据属性描述,它说版本为 3.5.0.0。根据对象浏览器,它说 dll 位于名为“Unity.3.5.1404.0”的文件夹中。注意dll是使用NuGet安装的,我安装的两组包分别叫做“Prism”和“Prism.UnityExtensions”。这是 Unity dll 所在的第二个。

What's interesting is that I actually started a new tutorial project to see if it'd fix the issue, and copied over some pieces of the projects from the original one. I was able to compile and run it the first time, but then I took out the section of code below from the App.config file just to see what would happen (dumb mistake...) and started getting the same error. This is even after I added it back to the App.config file. Note that the code below was in the App.config file when I created the project; it's not something I personally added later.

有趣的是,我实际上开始了一个新的教程项目,看看它是否能解决这个问题,并从原始项目中复制了一些项目。我第一次能够编译并运行它,但后来我从 App.config 文件中取出下面的代码部分只是为了看看会发生什么(愚蠢的错误......)并开始出现相同的错误。即使在我将它添加回 App.config 文件之后也是如此。请注意,当我创建项目时,下面的代码在 App.config 文件中;这不是我个人后来添加的。

Is there somewhere else I should look for rogue Unity dlls that might be causing issues? Is there something I need to add or remove so that the current Unity dll will work?

是否还有其他地方我应该寻找可能导致问题的流氓 Unity dll?是否需要添加或删除某些内容才能使当前的 Unity dll 正常工作?



Code where error is happening (on bootstrapper.Run; this is in App.xaml.cs):

发生错误的代码(在 bootstrapper.Run 上;这是在 App.xaml.cs 中):

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

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


Code:

代码:

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.1.0" newVersion="3.5.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>


Error:

错误:

System.IO.FileLoadException was unhandled HResult=-2146234304 Message=Could not load file or assembly 'Microsoft.Practices.Unity, Version=3.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Source=Microsoft.Practices.Prism.UnityExtensions FileName=Microsoft.Practices.Unity, Version=3.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 FusionLog="" StackTrace: 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\Google Drive\Karnaluks\Test Projects\EmailClient\EmailClient\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\Google Drive\Karnaluks\Test Projects\EmailClient\EmailClient\obj\Debug\App.g.cs:line 50 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(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.Threading.ThreadHelper.ThreadStart() InnerException: System.IO.FileLoadException HResult=-2146234304 Message=Could not load file or assembly 'Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) FileName=Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 FusionLog==== Pre-bind state information === LOG: DisplayName = Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///C:/Users/Roger/Google Drive/Karnaluks/Test Projects/EmailClient/EmailClient/bin/Debug/ LOG: Initial PrivatePath = NULL Calling assembly : Microsoft.Practices.Prism.UnityExtensions, Version=5.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\Roger\Google Drive\Karnaluks\Test Projects\EmailClient\EmailClient\bin\Debug\EmailClient.vshost.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: Redirect found in application configuration file: 3.5.0.0 redirected to 3.5.1.0. LOG: Post-policy reference: Microsoft.Practices.Unity, Version=3.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///C:/Users/Roger/Google Drive/Karnaluks/Test Projects/EmailClient/EmailClient/bin/Debug/Microsoft.Practices.Unity.DLL. WRN: Comparing the assembly name resulted in the mismatch: Build Number ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

System.IO.FileLoadException 未处理 HResult=-2146234304 Message=无法加载文件或程序集“Microsoft.Practices.Unity,版本=3.5.1.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)Source=Microsoft.Practices.Prism.UnityExtensions FileName=Microsoft.Practices.Unity,Version=3.5.1.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35 FusionLog=""StackTrace:在 Microsoft.Prism.Practices UnityExtensions.UnityBootstrapper.Run(Boolean runWithDefaultConfiguration) at Microsoft.Practices.Prism.Bootstrapper.Run() at EmailClient.App.OnStartup(StartupEventArgs e) in c: Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.IO.FileLoadException HResult=-2146234304 Message=无法加载文件或程序集“Microsoft.Practices.Unity,版本=3.5 .0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040) FileName=Microsoft.Practices.Unity,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35 FusionLog==== 预绑定状态信息 === LOG:DisplayName = Microsoft.Practices.Unity , Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (完全指定) LOG: Appbase = file:///C: /Users/Roger/Google Drive/Karnaluks/Test Projects/EmailClient/EmailClient/bin/Debug/ LOG:Initial PrivatePath = NULL 调用程序集:Microsoft.Practices.Prism.UnityExtensions,Version=5.0.1.0,Culture=neutral,PublicKeyToken= 31bf3856ad364e35。LOG:此绑定在默认加载上下文中启动。LOG:使用应用程序配置文件:C:\Users\Roger\Google Drive\Karnaluks\Test Projects\EmailClient\EmailClient\bin\Debug\EmailClient.vshost.exe.Config LOG:使用主机配置文件:LOG:使用机器配置文件来自 C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config。日志:在应用程序配置文件中发现重定向:3.5.0.0 重定向到 3.5.1.0。日志:政策后参考:Microsoft.Practices.Unity,版本=3.5.1.0,文化=中性,PublicKeyToken=31bf3856ad364e35 日志:正在尝试下载新的 URL 文件:///C:/Users/Roger/Google Drive/Karnaluks/Test Projects/EmailClient/EmailClient/bin/Debug/Microsoft.Practices.Unity.DLL。警告:比较程序集名称导致不匹配:内部版本号 ERR:无法完成程序集设置 (hr = 0x80131040)。探测终止。

   InnerException: 

回答by adminSoftDK

Have you tried clean and rebuild solution? Also maybe uninstalling the packages and then installing them again will help. That app.xaml code is added when you install Nuget packages.

您是否尝试过清理和重建解决方案?也可能卸载软件包然后再次安装它们会有所帮助。安装 Nuget 包时会添加该 app.xaml 代码。