无法加载文件或程序集“Newtonsoft.Json,Version=9.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项。

60

我有一个使用VS 2013在Windows 8.1上构建的WinJS项目。

最近,我通过创建空的Javascript通用Windows 10项目并将旧项目中的所有文件添加到其中来将该项目升级为通用Windows 10。

我有Windows运行时组件和SQLite类库。

我添加了通用Windows运行时组件和通用类库,并将所有旧项目的文件复制到相应的位置。

我成功解决了所有构建错误。

我安装了所有必需的SQLite-net、Universal Windows Platform的SQLite、Newtonsoft等软件包。

但是当我运行应用程序并调用Windows运行时组件中的本地方法时,会出现某种奇怪的错误,如下所示:

An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code.

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

enter image description here

Newtonsoft版本为: 9.0.1

我的Windows运行时组件的project.json文件如下:

  {
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

我的Visual Studio版本是:

在此输入图片描述

我尝试删除所有的Newtonsoft json并重新安装,但没有成功。


3
我之前遇到了一个新的ASP.NET Core应用程序的类似问题。结果发现其中一个被引用的库使用的Newtonsoft.Json版本比9.0.0.0低。因此,我升级了该库的版本,问题得到了解决。不确定您是否能在这里采取同样的措施解决问题。 - hbulens
嗨@hbulens,我尝试安装最新版本,但仍然会一遍又一遍地出现相同的错误。 - Kishor Bikram Oli
也许这个链接与你的问题相关 - https://github.com/aspnet/Home/issues/1609 - Sanket
25个回答

2

2
我在将Nuget包Newtonsoft.Json 12.0.0.2添加到我的两个.netstandard库项目中时遇到了这个问题,解决这个问题花费了我大部分时间。
异常 -: 无法加载文件或程序集“Newtonsoft.Json,Version=12.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”。系统找不到指定的文件。
解决方案 -: 我必须从Nuget中删除该包,并转到以下位置并按照下一步操作 -
步骤1. 转到位置“C:\ Users [UserName] .nuget \ packages \ newtonsoft.json \ 12.0.2 \ lib”,这里您将获得以前安装的所有Nuget版本。

enter image description here

步骤2:由于我想在我的.NET Standard 2.0库项目中使用它,所以我从这个位置复制了“netstandard2.0”文件夹,并将其粘贴到我首选的位置(**我通常保存第三方dll的地方)。
步骤3:现在我将DLL引用从这里添加到我的两个项目中,问题得到解决。
谢谢。

2

在我的情况下,这是与配置文件web.config有关的问题。当我更新了newton版本时,VS自动修复了我的web.config文件,使其指向新版本。但我上传到生产环境时,现有的web.config仍指向旧版本。

一旦我更新了web.config文件,它就重新开始正常工作了。

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

2
在2022年,我通过在第一个PropertyGroup中添加以下两行代码来解决这个问题。这是在我从packages.config迁移到csproj文件的PackageReference风格之后进行的。
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

我原本希望避免维护app.config文件。这是针对XF应用程序的,所以我不确定是否适用于Web应用程序。
此建议来自: 希望能帮助到某些人!

2
我遇到了一个非常相似的问题。我正在尝试在.NET DLL中使用Newtonsoft.Json.dll,就像我在计算机上成功地在.NET EXE中使用它一样。我在Visual Studio 2017中使用NuGet将Newtonsoft.Json添加到MyDll.dll中。MyExecutable.exe引用了MyDll.dll。从MyDll.dll内部调用Newtonsoft.Json方法时,会引发“System.IO.FileLoadException:无法加载文件或程序集“Newtonsoft.Json,Version=12.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其某个依赖项。找到的程序集清单定义与程序集引用不匹配(HRESULT异常:0x80131040)”。
我运行了Microsoft的fuslogvw.exe https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer 来检查正在加载什么,结果如下:
LOG: Post-policy reference: Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/MyExecutable/bin/Debug/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\MyExecutable\bin\Debug\Newtonsoft.Json.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=6.0.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: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

我的MyExecutable.exe没有引用或调用Newtonsoft.Json,但是我在添加任何Newtonsoft.Json引用到我的代码之前的MyExecutable源树的bin\Debug目录中发现了一个6.0.0.0 Newtonsoft.Json.dll。我不知道为什么会出现6.0.0.0 Newtonsoft.Json.dll。也许它被MyExecutable引用的另一个DLL所引用。我通过使用NuGet向MyExecutable添加对12.0.0.0 Newtonsoft.Json的引用来避免FileLoadException。
我本来以为在MyExecutable的App.config中使用绑定重定向作为替代方法可以避免MyExecutable引用Newtonsoft.Json,但是它没有起作用。 ...


继续上文: … <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> </configuration> - Alan Barksdale

2
我曾在ASP.NET Core项目中遇到类似的问题。 问题是bin/debug文件夹中的.config文件会生成以下内容:
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="6.0.0.0" newVersion="9.0.0.0" />
    <bindingRedirect oldVersion="10.0.0.0" newVersion="9.0.0.0" />
  </dependentAssembly>

如果我手动将第二个bindingRedirect更改为以下内容,则它可以正常工作:
<bindingRedirect oldVersion="9.0.0.0" newVersion="10.0.0.0" />

不确定为什么会发生这种情况。

我正在使用带有 .Net Core SDK 1.0.0-preview2-1-003177 的 Visual Studio 2015。


1

通过更新“Newtonsoft”版本可以解决该问题。

在Visual Studio 2015中,您可以右键单击“解决方案”,选择“管理解决方案的Nuget包”,搜索“Newtonsoft”,选择更高版本并单击更新。


1
我遇到了相同的错误:
无法加载文件或程序集“Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或其某个依赖项。所定位的程序集清单定义与程序集引用不匹配。 (HRESULT 的异常:0x80131040)
我卸载了 Newtonsoft.Json 并安装了这个版本。

enter image description here

这对我有效。

1

我在我的asp.net mvc项目中成功解决了这个问题,方法是通过包管理器更新Newton.Json版本(从旧版本9.0.0.0到新版本11.0.0.0)。


1
从该解决方案的所有项目中删除所有BIN和OBJ文件夹。当使用时,还要更新所有项目的包。删除包文件夹,重新构建和运行。这对我有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接