gpt4 book ai didi

c# - 运行时无法加载文件或程序集 'Newtonsoft.Json, Version=6.0.0.0 ...",间接依赖程序集继续寻找旧版本

转载 作者:行者123 更新时间:2023-12-03 15:51:43 24 4
gpt4 key购买 nike

我有一个 C# 项目,它直接引用了 Newtonsoft.Json 和一些其他引用(间接引用了 Newtonsoft.Json)。我想在所有地方将 Newtonsoft.Json 版本从 6.0.8 升级到最新的 12.0.2。

最初我得到了无法解决引用程序集的问题,但在搜索后我尝试将绑定(bind)放入 app.config 并且现在可以成功构建解决方案:

  <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

我的项目如下所示:
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>True</Private>
</Reference>

现在问题变成了在运行时调用库时,它会引发以下错误:

mymethod threw an exception, will try again: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified."



使用 AsmSpy ,似乎间接引用是根本原因:

Reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed Source: NotFound 6.0.0.0 by Microsoft.Azure.KeyVault, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Azure.KeyVault.WebKey, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35



通过启用诊断项目输出,构建时似乎很好:
2>  Unified primary reference "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". (TaskId:19)
2> Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Azure.KeyVault.2.0.6\lib\net45\Microsoft.Azure.KeyVault.dll" because AutoUnify is 'true'. (TaskId:19)
2> Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Azure.KeyVault.WebKey.2.0.4\lib\net45\Microsoft.Azure.KeyVault.WebKey.dll" because AutoUnify is 'true'. (TaskId:19)
2> Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Rest.ClientRuntime.2.3.2\lib\net45\Microsoft.Rest.ClientRuntime.dll" because AutoUnify is 'true'. (TaskId:19)
2> Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.1\lib\net45\Microsoft.Rest.ClientRuntime.Azure.dll" because AutoUnify is 'true'. (TaskId:19)
2> Resolved file path is "F:\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll". (TaskId:19)
2> Reference found at search path location "{HintPathFromItem}". (TaskId:19)
2> Found related file "F:\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.xml". (TaskId:19)
2> The ImageRuntimeVersion for this reference is "v4.0.30319". (TaskId:19)

然后我尝试将所有这 4 个 nuget 包升级到最新版本并在我的解决方案中重新安装包,清理解决方案并重建,再试一次但仍然失败。

有没有人遇到过同样的问题以及如何解决这个问题?

最佳答案

只需从所有地方删除任何 Newtonsoft.Json 链接:

  • *.csproj
  • 包.config
  • 包文件夹
  • bin 文件夹
  • 你能找到的任何其他地方

  • 关闭项目,然后再次安装包:Install-Package Newtonsoft.Json -12.0.0

    此外,如果您有指向它的链接,请检查任何其他项目中的 Newtonsoft.Json 版本。

    关于c# - 运行时无法加载文件或程序集 'Newtonsoft.Json, Version=6.0.0.0 ...",间接依赖程序集继续寻找旧版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276015/

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