程序集引用的位置在哪里?

Where is the location of an Assembly reference?

我遇到 Newtonsoft 的 FileLoadException,在我安装 11.0.2.0 时寻找版本 6.0.0.0。 packages.config 设置为查找较新的版本,web.config 也是如此。参考中的 .dll 版本是 11,我的解决方案之外的框架文件夹中的临时 ASP 文件也是如此。我试过强制卸载 newtonsoft 并在包管理器控制台中安装最新版本。

在代码内外,我找不到任何定义版本 6.0.0.0 的地方。这是在哪里设置的?

这是融合日志(你可以看到它在失败之前尽一切可能获得匹配):

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed(Fully-specified)
LOG: Appbase = file:///C:/Repos/MyProjectPath/
LOG: Initial PrivatePath = C:\Repos\MyProjectPath\bin
Calling assembly : System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Repos\MyProjectPath\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/MyProjectFolder/20c630f1/c09fb4a6/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/MyProjectFolder/20c630f1/c09fb4a6/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Repos/MyProjectPath/bin/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

该解决方案可能对大多数有程序集引用问题的人没有用,但这是我必须为 1000 人中有 1 人有类似问题的人做的。

构建正确地将版本 6.0.0.0 引用从调用程序集 (System.Net.Http.Formatting) 重定向到 11.0.0.0,如构建日志的详细输出所示:

Unified primary reference "Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". (TaskId:108)
7>      Using this version instead of original version "6.0.0.0" in "C:\Repos\MySolutionFolder\NugetPackages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll" because of a binding redirect entry in the file "Web.config". (TaskId:108)
7>      Using this version instead of original version "6.0.0.0" in "C:\Repos\MySolutionFolder\NugetPackages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll" because of a binding redirect entry in the file "Web.config". (TaskId:108)
7>      Using this version instead of original version "6.0.0.0" in "C:\Repos\MyReferencedProjectPath\bin\Debug\MyLibrary.dll" because of a binding redirect entry in the file "Web.config". (TaskId:108)
7>      Resolved file path is "C:\Repos\MySolutionFolder\NugetPackages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll". (TaskId:108)

然而,即使 web.config bindingRedirect 是正确的,在 web.config 的应用程序设置中有一个元素,如果不在最终版本中删除,将阻止重定向在运行。以下是 web.config 中的违规元素:

<?ap-config target="add[@key='Environment']/@value"
  value="{@environment}"
  when="true()" ?>

我的特定解决方案涉及一个 post-build 事件脚本,该脚本将从文件中删除这一行,然后再将其放入我解决方案中每个 Web 项目的构建输出文件夹中。您的特定应用程序可能需要不同的解决方案,但关键要点是即使构建输出声称程序集引用是通过重定向找到的,web.config 中的其他内容导致构建版本在运行时中断。

我不知道如何更深入地调试来跟踪这个问题,所以你只需要知道即使是成功的构建日志也不能说明全部情况。