c# - 双异常不一致(System.IO.FileNotFoundException / System.Runtime 程序集版本)
问题描述
在 WPF 应用程序中,我Prism.Unity.UnityBootstraper
在此上下文中创建了一个子类 Bootstrapper:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bootstapper = new Bootstrapper();
bootstapper.Run();
}
构建很好,但是在执行时我有一个双重例外:
System.IO.FileNotFoundException:'无法加载文件或程序集'System.Runtime,版本 = 4.1.1.0,文化 = 中性,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。该系统找不到指定的文件。'
我有以下内部异常,其中版本不同:
FileNotFoundException:无法加载文件或程序集“System.Runtime,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。该系统找不到指定的文件。
我重新安装了 nuget 包,并尝试仅使用此代码创建一个孤立的用例,但我无法重现异常。
发生了什么事,我该如何解决这个地狱?
为了进一步调查,我激活了 FusionLog:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion]
"ForceLog"=dword:00000001
"LogFailures"=dword:00000001
"LogResourceBinds"=dword:00000001
"LogPath"="C:\\Logs\\Fusion\\"
但是该实用程序fuslogvs.exe
并不实用。所以我选择了powershell:
select-string "operation failed" *htm -ca | % {"$($_.filename):$($_.line)"}
从我的程序集的日志目录中,在c:/logs/fusion
. 我发现了 3 个问题,尤其是:
System.Runtime,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a.HTM:操作失败。
在文件中,我得到:
LOG: AppName = ShutterModeler.exe
Calling assembly : Prism, Version=6.3.0.0, Culture=neutral, PublicKeyToken=40ee6c3a2184dc59.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\soleil\Documents\Visual Studio 2017\Projects\ShutterModeler\x64\Debug\ShutterModeler.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.0.0 redirected to 4.1.1.0.
LOG: Post-policy reference: System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/soleil/Documents/Visual Studio 2017/Projects/ShutterModeler/x64/Debug/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/soleil/Documents/Visual Studio 2017/Projects/ShutterModeler/x64/Debug/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/soleil/Documents/Visual Studio 2017/Projects/ShutterModeler/x64/Debug/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/soleil/Documents/Visual Studio 2017/Projects/ShutterModeler/x64/Debug/System.Runtime/System.Runtime.EXE.
LOG: All probing URLs attempted and failed.
实际上,目录构建目标中没有System.Runtime.DLL
文件。
但是,System.Runtime
已安装 (v4.3),并存在于$(SolutionDir)\packages
. 在里面我找不到 dotnet 4.7.2 目标:
Name
----
MonoAndroid10
MonoTouch10
net45
net462
portable-net45+win8+wp80+wpa81
win8
wp80
wpa81
xamarinios10
xamarinmac20
xamarintvos10
xamarinwatchos10
我的 WPF 项目的目标是 .net 4.7.2。
正如 Alex Ghiondea 所建议的那样 - MSFT
我删除了:
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
from app.config
,并且不再需要构建后的手动副本作为解决方法:
xcopy /y "$(SolutionDir)packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll" "$(TargetDir)"
解决方案
看起来您在应用程序的 app.config 中有一个针对 System.Runtime 的程序集绑定重定向。请删除它,我希望这个错误会消失
推荐阅读
- css - 为什么在 CSS 中不起作用 [选择父级]
- python - 假设 unicode_literals,如何安全地评估文字的表示?
- sql - 获取“将数据类型 varchar 转换为数字时出错。” 即使在转换之后
- python - Python 没有多次运行我的 For 循环
- angular - 如何在 Angular 7 中的 URL 中添加尾部斜杠,而不会在刷新时出现 404 问题?
- c# - 将日期发布到 Asp.net 核心 API
- c# - 在 C# 中访问 WindowsInstaller.Installer COM 对象
- powerbi - DAX 确定 Power BI 的库存产品数量
- reactjs - React async and await throwing error 'Uncaught ReferenceError: regeneratorRuntime is not defined'
- r - 基于一列连接两个数据集