Newtonsoft.Json与Assembly冲突

23

我需要加载两个版本的程序集Newtonsoft.Json,分别是4.0.8.0版本和4.5.0.0版本。我的当前配置文件:

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

但需要的版本是:旧版本 4.0.8.0 和新版本 4.5.0.0。

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

我从Package Console安装了最新版本的Newtonsoft,但是它给了我一个错误:

Error 80 无法加载文件或程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其某个依赖项。位于程序集清单定义的已定位程序集与程序集引用不匹配。(来自 HRESULT: 0x80131040 的异常)


1
我检查了JSON.net NuGet的发布信息,但是找不到4.5.0.0版本的发布。 http://www.nuget.org/packages/newtonsoft.json/ 此外,如果您已经拥有库的最新版本,为什么还需要使用旧版本呢? - Manvik
9
为什么我记得在 NuGet 之前一切都变得容易多了? - Ronnie Overby
2个回答

39

今天我遇到了这个问题,我在这个链接中找到了解决方法。

基本上是更新Newtonsoft.Json包。并在web.config文件中注册该程序集。

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

你从哪里获取publicKeyToken的? - Lay González
1
当我尝试构建我的应用程序时,我在错误列表控制台中得到了publicKeyToken,所以我复制了它,但这是另一种获取它的方式。https://dev59.com/7XI-5IYBdhLWcg3wta7w如果您在web.config中有其他引用,您可能会注意到它们是相等的,因此您可以从另一个程序集中复制令牌。 - nramirez
这有点混乱,但最终我使它工作了。首先我将我的项目从Newtonsoft 9.x更新到12.0.2版本,可惜没成功。然后我回退到9.x并重新启动了VS,还是不行。接着我又试了从9.x再升级到12.0.2版本,结果还是失败了。最后我尝试了从12.0.2版本降级到12.0.1版本,然后它就能正常工作了。/我真的是无语了。至少现在它能正常运行了。 - Christopher

8

我在安装SignalR到我的项目后遇到了同样的问题。首先,我更新了Newtonsoft.Json到最新版本,然后我将dependentAssembly添加到了我的web.config文件中。但是,即使在我的packages中声明了版本6.0.8,我仍然需要在新版本中将值设置为6.0.0.0。

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

这个方法在版本8.0.3上对我有效。在我把newVersion改为8.0.0.0之前,它一直无法工作。 - Kevin Kulla
2
刚刚尝试了一下,使用12.0.1版本时出现了问题。我将其更改为12.0.0.0版本,问题得到了解决。谢谢! - guyfromfargo

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