gpt4 book ai didi

asp.net - 使用多个项目时出现 Newtonsoft.Json.dll 问题

转载 作者:行者123 更新时间:2023-12-02 03:34:14 25 4
gpt4 key购买 nike

我看到一些非常奇怪的行为,我无法更正这些行为与 Newtonsoft.Json.dll 的引用有关。我有一个使用以下项目设置的示例解决方案:

  • JsonProblem.Core
  • JsonProblem.CauseProblem(引用 JsonProblem.Core)
  • JsonProblem.Web(引用 JsonProblem.Core 和 JsonProblem.CauseProblem)

在 JsonProblem.Core 和 JsonProblem.Web 中,我添加了“Microsoft ASP.NET Web API 2.2”NuGet 包。在 JsonProblem.Core 中,我创建了一个 web api。如果我构建 JsonProblem.Core 并从 JsonProblem.Web 运行一个页面,一切都会按预期进行。

现在,如果我构建 JsonProblem.CauseProblem 并尝试在 JsonProblem.Web 中查看页面,我会收到以下错误。

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

如果我重建 JsonProblem.Core,错误就会消失。再一次,如果我构建 JsonProblem.CauseProblem 之后没有构建 JsonProblem.Core(即使 JsonProblem.CauseProblem 依赖于 JsonProblem.Core),我也会收到错误消息。 JsonProblem.CauseProblem 的构建以某种方式导致 Newtonsoft.Json 版本 4.5.11 被复制到 JsonProblem.Web bin 目录,覆盖版本 6.0.3。我很确定我的绑定(bind)重定向设置正确,因为我在 JsonProblem.Web web.config 和 JsonProblem.Core 和 JsonProblem.CauseProblem 的 app.config 文件中有以下内容:

<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

所以我对这种奇怪行为的原因一头雾水。我已经在 2 个项目中复制了它。当我构建 JsonProblem.CauseProblem 时,绑定(bind)重定向似乎被忽略了。我可以解决它,但我担心导致此行为的任何错误或功能可能会改变后台中的其他引用,这可能会导致问题。


编辑 - 正如 tizzy 建议的那样,我使用了 fuslogvw 工具。这是日志中生成的内容。但是,我不确定如何解释这一点,因为日志没有告诉我在构建时发生了什么以覆盖我的应用程序网站目录中的 Newtonsoft.Json.dll 版本。

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Program Files\IIS Express\iisexpress.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Newtonsoft.Json | Domain ID: 5
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/John/Desktop/JsonProblem/JsonProblem.Web/
LOG: Initial PrivatePath = C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin
LOG: Dynamic Base = C:\Users\John\AppData\Local\Temp\Temporary ASP.NET Files\vs\3661babd
LOG: Cache Base = C:\Users\John\AppData\Local\Temp\Temporary ASP.NET Files\vs\3661babd
LOG: AppName = 3b3fd45
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\web.config
LOG: Using host configuration file: C:\Users\John\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\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/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\web.config
LOG: Using host configuration file: C:\Users\John\Documents\IISExpress\config\aspnet.config
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.5.0.0 redirected to 6.0.0.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: The post-policy assembly reference requires probing again.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

最佳答案

我的行为与上述问题描述完全相同,除了当我重建我的类库(即 JsonProblem.Core)时,它会引入 Newtonsoft.dll 4.5.11,即使这个类库没有直接引用或指示对 NewtonSoft 的引用的 Nuget 包。

在可视为 Visual Studio 2015 错误的情况下,类库重建将不可见引用的 DLL 复制到引用项目 bin 文件夹(当前未构建),覆盖引用项目中的 DLL,破坏它们。

IOW,例如,构建项目 Y(引用 JsonProblem.Core),明显成功。构建项目 X(引用 JsonProblem.Core),这会破坏项目 Y。构建项目 Y,现在这会破坏项目 X。无限循环: while( sane ) { build();

在我的例子中,我在该库中引用的唯一第 3 方库是 Gibraltar.Agent 和 Gibraltar.Agent.Web.Mvc(报告不依赖于 NewtonSoft。在项目引用中右键单击它们并选择“查找Code Dependent on this module”显示我不需要引用第二个。

我在 Nuget 中卸载了 Gibraltar.Agent.Web.Mvc,所有问题都消失了。两天的 DLL Hell 心痛已经结束。

关于asp.net - 使用多个项目时出现 Newtonsoft.Json.dll 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com