C# Newtonsoft.json 汇编包版本不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17236342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:55:11  来源:igfitidea点击:

Newtonsoft.json assembly package version mismatch

c#dependenciesnugetjson.netsocketio4net

提问by Bitsian

I am trying to use SocketIO4Netto create socket.io client in .net. Itseems SocketIO4Net has a dependency of Newtonsoft.Json >= 4.0.8. I also am using PushSharplibrary which has a Newtonsoft.Json dependency of >= 4.5.10. I got NewtonSoft.Json 4.5.11 when i first installed PushSharp and I thought this version should support SocketIO4Net as well since its a higher version but i get this error whenever am trying to connect to socket.io server.

我正在尝试使用SocketIO4Net在 .net 中创建 socket.io 客户端。看来 SocketIO4Net 依赖于 Newtonsoft.Json >= 4.0.8。我还使用PushSharp库,它具有 >= 4.5.10 的 Newtonsoft.Json 依赖项。当我第一次安装 PushSharp 时,我得到了 NewtonSoft.Json 4.5.11,我认为这个版本也应该支持 SocketIO4Net,因为它是一个更高的版本,但是每当我尝试连接到 socket.io 服务器时我都会收到这个错误。

Could not load file or assembly 'Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

无法加载文件或程序集“Newtonsoft.Json,版本=4.0.8.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)

I have been banging my head all day with these dependency issues, I would be very grateful if someone can point me in the right direction.

我整天都在为这些依赖性问题而烦恼,如果有人能指出我正确的方向,我将不胜感激。

回答by ChrisBint

Put in an assembly redirect in your app/web.config;

在 app/web.config 中放入程序集重定向;

   <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0" />
      </dependentAssembly>

Please note the versions numbers need to match the version you have installed.

请注意版本号需要与您安装的版本相匹配。

回答by Miroslav Bajto?

You can modify assembly-binding configuration and add a redirect. See Redirecting Assembly Versionson MSDN.

您可以修改程序集绑定配置并添加重定向。请参阅MSDN 上的重定向程序集版本

Basically you want to add following snippet to your app.configor web.configfile:

基本上,您想将以下代码段添加到您的app.configweb.config文件中:

<configuration>
   <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="Newtonsoft.Json"
                           publicKeyToken="30ad4fe6b2a6aeed"
                           culture="neutral" />
         <!-- 
           Assembly versions can be redirected in application, 
           publisher policy, or machine configuration files.
         -->
         <bindingRedirect oldVersion="1.0.0.0-4.5.11.0" newVersion="4.5.11.0"/>
       </dependentAssembly>
     </assemblyBinding>
   </runtime>
</configuration>

EDIT

编辑

Why do you need to redirect assembly versions? Even though SocketIO4Net supports newer versions of Newtonsoft.Json, it was compiled against a single version (4.0.8 in your case). This version is stored in the DLL and it is used to load DLLs SocketIO4Net depends on.

为什么需要重定向程序集版本?尽管 SocketIO4Net 支持较新版本的 Newtonsoft.Json,但它是针对单个版本(在您的情况下为 4.0.8)编译的。此版本存储在 DLL 中,用于加载 SocketIO4Net 依赖的 DLL。

Note that NuGet dependencies are not the same as DLL/runtime dependencies - NuGet dependency on Newtonsoft.Json >= 4.0.8 only means that you will be allowed to install SocektIO4Net into a project that has a newer version of Newtonsoft.Json, it has nothing to do with runtime settings.

请注意,NuGet 依赖项与 DLL/运行时依赖项不同 - NuGet 依赖 Newtonsoft.Json >= 4.0.8 仅意味着您将被允许将 SocektIO4Net 安装到具有较新版本 Newtonsoft.Json 的项目中,它具有与运行时设置无关。

That being said, recent NuGet versions should add assembly-binding-redirects automatically for you if your project has app.config or web.config file.

话虽如此,如果您的项目有 app.config 或 web.config 文件,最近的 NuGet 版本应该会自动为您添加程序集绑定重定向。

回答by ZeroDotNet

Found solution, try with:

找到解决方案,尝试:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <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>
    </assemblyBinding>
</runtime>

回答by Venkat

Got the above Error: in Visual Studio 2013 To Fix: In package mamnager Execute: Install-package newtonsoft.json This will add a new line in packages.config <package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />Remove the previous line which might point to previous version on packages.config. Delete the old version's directory on the packagers directory. Remove the reference of NewtonSoft.Json and readd it pointing to the latest version. Root webconfig will have the following <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />once everything is done. Close and reopen visual studio. This should fix it. I had the same error when installing PM> install-package durandal.starterkit I used the above method to fix.

得到上述错误:在 Visual Studio 2013 中修复:在包管理器中执行:安装包 newtonsoft.json 这将在包中添加一个新行 <package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />。删除 packagers 目录中旧版本的目录。删除 NewtonSoft.Json 的引用并读取它指向最新版本。<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />一切完成后,根 webconfig 将具有以下 内容。关闭并重新打开 Visual Studio。这应该解决它。我在安装 PM> install-package durandal.starterkit 时遇到了同样的错误我使用上述方法修复。

回答by Ippo

I have fixed this issue easily: I had not copied the xml configuration file from the compilation folder.

我已经轻松解决了这个问题:我没有从编译文件夹中复制 xml 配置文件。

I just made sure that the xml configuration file was also included along with my program and everything worked fine!

我只是确保 xml 配置文件也包含在我的程序中,并且一切正常!

回答by Kickass

Had this same issue.

有同样的问题。

Just resolved it.

刚刚解决了。

It happened after NuGet was used to install Ext.NET which has a dependency for Newtonsoft.JSON.
There was already a Newtonsoft.JSON.dll file in /bin (and obviously a reference to it in the web.config file) folder without checking I started the NuGet Package-Install procedure while debugging(so the file probably had a lock).

它发生在 NuGet 用于安装 Ext.NET 之后,该 Ext.NET 对 Newtonsoft.JSON 有依赖性。
/bin 文件夹中已经有一个 Newtonsoft.JSON.dll 文件(显然在 web.config 文件中引用了它)文件夹中没有检查我在调试时启动了 NuGet Package-Install 过程(所以文件可能有锁)。

On the runtime error window it will tell you on the stack trace what part of the manifest it has a problem with, mine was major version so I checked the install package version. and it was 1 major version out. Found the original NuGet file under: "[physical path]/../packages/Newtonsoft.Json.[version]/lib/[.net version]/"

在运行时错误窗口中,它会在堆栈跟踪中告诉您清单的哪一部分有问题,我的是主要版本,所以我检查了安装包版本。它是 1 个主要版本。在:“[物理路径]/../packages/Newtonsoft.Json.[version]/lib/[.net version]/”下找到原来的NuGet文件

Both Manifest and Library was there so copied it into /bin folder, updated the root web.config assembly information and it worked.

Manifest 和 Library 都在那里,因此将其复制到 /bin 文件夹中,更新了根 web.config 程序集信息并且它起作用了。

Code samples: Before

代码示例:之前

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

After

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

Hope this helps

希望这可以帮助

回答by TyCobb

Just had this happen with TeamCityand I imagine others will soon experience this. This probably applies to most build serversthat pull NuGet packages.

TeamCity刚刚发生了这种情况,我想其他人很快就会体验到这一点。这可能适用于大多数提取 NuGet 包的构建服务器

All the answers that say to do redirects are correct. However, you need to still define the correct version number. My project was using Newtonsoft.Json 7.0, however, they had just released 8.0and TeamCity was pulling down 8.0which was causing issues only on the server and not locally. All my redirects were set to 7.0.

所有说要进行重定向的答案都是正确的。但是,您仍然需要定义正确的版本号。我的项目正在使用Newtonsoft.Json 7.0,但是,他们刚刚发布8.0并且 TeamCity 正在拉下,8.0这仅在服务器上而不是在本地引起问题。我所有的重定向都设置为7.0.

Make sure that the deployed application is actually getting the correct version from NuGet and not just the latest and greatest. Or update your config to point to the newest version.

确保部署的应用程序实际上是从 NuGet 获取正确版本,而不仅仅是最新和最好的。或者更新您的配置以指向最新版本。

回答by Gerson C Filho

In my case, I removed the package with NuGet and installed a fresh one. Then, remove the reference from References and add again manually. Works like charm. Hope resolve for you.

就我而言,我使用 NuGet 删除了该包并安装了一个新包。然后,从 References 中删除引用并再次手动添加。像魅力一样工作。希望为你解决。

回答by Ultroman the Tacoman

I was working on an old project recently. I needed to update our Newtonsoft.Json.dll, since I had to utilize a "new" API which required a newer version, but I still had other DLLs that required the old version.

我最近在做一个旧项目。我需要更新我们的 Newtonsoft.Json.dll,因为我必须使用需要更新版本的“新”API,但我仍然有其他需要旧版本的 DLL。

bindingRedirectyou say? Nope. It kept complaining about the manifest mismatch.

你说bindingRedirect?不。它一直抱怨清单不匹配。

Separate codeBasetags? Nope. It kept complaining about the manifest mismatch.

单独的codeBase标签?不。它一直抱怨清单不匹配。

The problem was, apparently, that the old version of Newtonsoft.Json.dll (3.0.0.0) does NOT have a PublicKeyToken, but the "new" version (4.5.7.1) DOES have a PublicKeyToken. Therefore they couldn't share the same dependentAssembly-tag.

问题显然是,旧版本的 Newtonsoft.Json.dll (3.0.0.0) 没有 PublicKeyToken,但“新”版本(4.5.7.1) 有 PublicKeyToken。因此它们不能共享相同的dependentAssembly-tag。

This is what I ended up with:

这就是我最终的结果:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="" culture="neutral"/>
    <codeBase version="3.0.0.0" href="bin\Newtonsoft_Old\Newtonsoft.Json.dll" />
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
    <codeBase version="4.5.0.0" href="bin\Newtonsoft.Json.dll" />
</dependentAssembly>

回答by EgoistDeveloper

The above solutions are correct but there is one more point that should not be forgotten: the app.configcontent was the same as the above solutions.

以上解决方案是正确的,但还有一点不能忘记:app.config内容与上述解决方案相同。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

But it's a good idea to check if it's up to date. In my case, Newtonsoft.JSON (v.6.0.4)has come to depend on another package.

但最好检查一下它是否是最新的。就我而言,Newtonsoft.JSON (v.6.0.4)已经开始依赖于另一个包。

enter image description here

在此处输入图片说明

There are two option;

有两种选择;

  1. Update (Newtonsoft.JSON package)last versions.
  2. Update app.config file in the version numbers.
  1. 更新(Newtonsoft.JSON 包)最新版本。
  2. 更新版本号中的 app.config 文件

And last advice, if you are working with more than one project, eg. exe-dll and check both versions if there is Newtonsoft.JSON.

最后一个建议,如果您正在处理多个项目,例如。exe-dll 并检查两个版本是否有 Newtonsoft.JSON。