【问题标题】:Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0 in combination with Microsoft.AspNet.WebApi.Client无法结合 Microsoft.AspNet.WebApi.Client 加载文件或程序集 'Newtonsoft.Json, Version=6.0.0.0
【发布时间】:2016-06-05 09:40:18
【问题描述】:

我正在编写一个类库(称为 mylibrary.dll),它本身引用了更多库 -> 使用以下 DLL(取自 package.config 以获取版本概述):

<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
  <package id="System.Data.SQLite" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.Core" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.EF6" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.Linq" version="1.0.99.0" targetFramework="net45" />
  <package id="UnmanagedExports" version="1.2.7" targetFramework="net45" />`

mylibrary.dll 是一个包装器,它将一些托管代码公开给需要非托管代码的调用者(换句话说,需要本地 DLL 条目)。

如果我通过 NUnit 测试方法测试 mylibrary.dll 的公共接口,则完全没有错误。但是,如果我通过 targetapplication.exe 中的相同接口调用相同的方法,我会识别出以下情况:

  • 测试方法A:调用简单的JSON转字符串操作(利用 Newtonsoft.JSON 库)并且运行良好。
  • 测试方法 B:调用 执行 PostAsync 的方法,此外

var vResponseObject = 等待 vResponse.Content.ReadAsAsync>().ConfigureAwait(false);

ReadAsAsync 调用在幕后使用 Newtonsoft.JSON 反序列化 &lt;T&gt; 类型的对象。似乎这个函数是产生错误的函数:

无法加载文件或程序集 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=.......' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

HTTPContent.ReadAsAsync 由 Microsoft.AspNet.WebApi.Client(扩展 System.Net.Http)提供,它本身依赖于 Newtonsoft.JSON 版本 6.0.x(请参阅此包的 NuGet 依赖项)。未安装 6.0.x 版,而是安装了 8.0.x 版。所以需要在 app.config 中管理的组件绑定重定向:

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

现在我不知道如何解决这个问题。在我的项目中,Microsoft.AspNet.WebApi.Client 是唯一一个引用 Newtonsoft.JSON 的其他库(版本 6.0.x,与错误告诉我的版本相同)。似乎绑定重定向被简单地忽略了。因为它不是“找不到文件异常”,所以我认为它能够定位到版本 8 的 dll,但预期是 6,对吧?所有 DLL 都与 targetapplication.exe 位于同一目录中

更新部分解决方案: 作为一种解决方法,我能够通过 System.Net.Http.Formatting.dll 避免 Newtonsoft.Json 的外部调用

//vResponseObject = await vResponse.Content.ReadAsAsync<ApiResponse<T>>().ConfigureAwait(false);
string vStr = await vResponse.Content.ReadAsStringAsync();
vResponseObject = JsonConvert.DeserializeObject<ApiResponse<T>>(vStr);

但是,如果我必须围绕 mydll -> thirdparty.dll -> anotherthirdparty.dll 之类的调用编写代码,这对于进一步开发来说确实不是有效的解决方案

【问题讨论】:

  • 错误消息中 Newtonsoft.Json 的 PublicKeyToken 是否与您的 app.config 中的内容匹配?
  • 您好,感谢您的建议,但是是的,错误消息中的 publicKeyToken 与 assemblyIdentity Tag 中的相同。
  • 你试过stackoverflow.com/questions/3490327/…的所有建议了吗?

标签: c# dll reference json.net .net-assembly


【解决方案1】:

我刚刚使用 Newtonsoft 7.0.1 版本解决了这个问题。我替换了 web.config 文件中的旧绑定,这很奇怪:

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

收件人:

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

最后,将 Newtonsoft 引用从它指向的任何内容更改为 NuGet 包 v8 中的版本。您很可能指向许多 Newton.Json DLL 之一,即 C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\Newtonsoft.Json.6.0.3\lib\net40。我不知道它为什么会这样,但我只遇到过这个库的问题。

【讨论】:

  • 您好,正如我在原帖中提到的,程序集绑定部分看起来与您改进的部分完全一样。并且 Newtonsoft.Json 的引用指向 \packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll,本地副本设置为 true。我想知道为什么测试路由工作得很好而生产应用程序失败(同一台机器,我将库从构建文件夹中复制,在测试期间没有发生故障,到生产文件夹,在那里发生错误......)。跨度>
  • @stev-e 你能在哪里解决它?如果是这样,如何?谢谢。
【解决方案2】:

改变

 <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />

 <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />

并检查您的依赖项 Newtonsoft.Json。如果它有黄色图标,请将其删除并手动添加。 (确保添加 6.0 版本)。 (您的项目文件夹或其他项目中应该有 dll)。如果这不起作用通过 NuGet 添加旧版本(我更喜欢自己手动添加它们)。

dll的位置通常是:C:\Users\Username\Documents\Visual Studio 2013\Projects\ProjectFolder\packages\Newtonsoft.Json.6.0.8\lib\net45(或任何版本的.net)

【讨论】:

  • 您好,参考有效,没有黄色图标。版本 6 是不可能的,它需要 >= 8
【解决方案3】:

删除旧的引用并通过nuget获取新的dll,我认为这将解决大多数场景。

【讨论】:

  • 你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 2021-02-02
  • 2017-11-15
  • 2017-03-13
相关资源
最近更新 更多