asp.net-mvc 在新的 ASP.NET MVC 5 项目中更新 NuGet 包引用后,如何修复程序集版本与 JSON.NET 的冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21498074/
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-09-08 03:30:07  来源:igfitidea点击:

How can I fix assembly version conflicts with JSON.NET after updating NuGet package references in a new ASP.NET MVC 5 project?

asp.net-mvcjson.netnugetvisual-studio-2013asp.net-mvc-5.1

提问by Jim Lamb

I created a new ASP.NET MVC 5 web project in VS 2013 (Update 1) then updated all NuGet packages. When I build the project, I get the following warning:

我在 VS 2013(更新 1)中创建了一个新的 ASP.NET MVC 5 web 项目,然后更新了所有 NuGet 包。当我构建项目时,我收到以下警告:

warning MSB3243: No way to resolve conflict between "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" and "Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed".

警告 MSB3243:无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”和“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”之间的冲突

When I check the web.config, however, I see that a binding redirect is in place:

但是,当我检查 web.config 时,我看到绑定重定向已到位:

  <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>

Which is exactly what the warning advises.

这正是警告所建议的。

How can I fix this warning?

我该如何解决这个警告?

回答by Jim Lamb

Here the steps I used to fix the warning:

这是我用来修复警告的步骤:

  • Unload project in VS
  • Edit .csproj file
  • Search for all references to Newtonsoft.Json assembly
    • Found two, one to v6 and one to v5
    • Replace the reference to v5 with v6
  • Reload project
  • Build and notice assembly reference failure
  • View References and see that there are now two to Newtonsoft.Json. Remove the one that's failing to resolve.
  • Rebuild - no warnings
  • 在 VS 中卸载项目
  • 编辑 .csproj 文件
  • 搜索所有对 Newtonsoft.Json 程序集的引用
    • 找到两个,一个到 v6,一个到 v5
    • 用 v6 替换对 v5 的引用
  • 重新加载项目
  • 构建并通知程序集引用失败
  • 查看参考资料并看到 Newtonsoft.Json 现在有两个。删除无法解决的那个。
  • 重建 - 没有警告

回答by McGaz

I had this problem because I updated packages, which included Microsoft.AspNet.WebApi that has a reference to Newtonsoft.Json 4.5.6 and I already had version 6 installed. It wasn't clever enough to use the version 6.

我遇到这个问题是因为我更新了包,其中包括 Microsoft.AspNet.WebApi,它引用了 Newtonsoft.Json 4.5.6 并且我已经安装了版本 6。使用版本 6 不够聪明。

To resolve it, after the WebApi update I opened the Tools > NuGet Package Manager > Pacakge Manager Console and ran:

为了解决这个问题,在 WebApi 更新之后,我打开了 Tools > NuGet Package Manager > Pacakge Manager Console 并运行:

 Update-Package Newtonsoft.Json

The log showed that the 6.0.x and 4.5.6 versions were all updated to the latest one and everything was fine.

日志显示6.0.x和4.5.6版本都更新到最新版本,一切正常。

I have a feeling this will come up again.

我有一种感觉,这会再次出现。

回答by szmulder

I found to delete this section from the project file fix the problem.

我发现从项目文件中删除此部分可以解决问题。

<ItemGroup>
<Reference Include="Newtonsoft.Json">
  <HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>

回答by ZeroDotNet

If none of the above works, try using this in web.config or app.config:

如果以上都不起作用,请尝试在 web.config 或 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-6.0.0.0" newVersion="6.0.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

回答by Jeremy Ray Brown

I upgraded from Newtonsoft.Json 11.0.1 to 12.0.2. Opening the project file in Notepad++ I discovered both

我从 Newtonsoft.Json 11.0.1 升级到 12.0.2。在 Notepad++ 中打开项目文件我发现了两者

<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>
    </Reference>

and

<ItemGroup>
    <Reference Include="Newtonsoft.Json">
      <HintPath>..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
  </ItemGroup>

I deleted the ItemGroup wrapping the reference with the hint path to version 11.0.1.

我删除了用版本 11.0.1 的提示路径包装引用的 ItemGroup。

These issues can be insanely frustrating to find. What's more, developers often follow the same steps as previous project setups. The prior setups didn't encounter the issue. For whatever reason the project file occasionally is updated incorrectly.

发现这些问题可能会非常令人沮丧。此外,开发人员通常遵循与以前的项目设置相同的步骤。之前的设置没有遇到这个问题。无论出于何种原因,项目文件偶尔会更新不正确。

I desperately wish Microsoft would fix these visual studio DLL hell issues from popping up. It happens far too often and causing progress to screech to a halt until it is fixed, often by trial and error.

我非常希望 Microsoft 能够修复这些 Visual Studio DLL 地狱问题的出现。它发生得太频繁了,导致进度戛然而止,直到它被修复,通常是通过反复试验。

回答by Jeremy Ray Brown

The final solution to your assembly redirect errors

程序集重定向错误的最终解决方案

Okay, hopefully this should help resolve any(sane) assembly reference discrepancies ...

好的,希望这应该有助于解决任何(理智的)程序集参考差异......

  1. Check the error.
  1. 检查错误。

Surf to the website

浏览网站

  1. Check web.config after the assembly redirect. Create one if not exists.
  1. 在程序集重定向后检查 web.config。如果不存在则创建一个。

Existing web.config assembly redirect

Existing web.config assembly redirect

  1. Right-click the reference for the assembly and choose Properties.
  1. 右键单击装配体的参考并选择“特性”。

Assembly in the Reference list, in the relevant project

Assembly in the Reference list, in the relevant project

  1. Check the Version (not Runtime version) in the Properties table. Copy that.
  1. 检查属性表中的版本(不是运行时版本)。收到。

Properties table showing Version of assembly

Properties table showing Version of assembly

  1. Paste into the newVersion attribute.
  1. 粘贴到 newVersion 属性中。

web.config assembly redirect with updated newVersion

web.config assembly redirect with updated newVersion

  1. For convenience, change the last part of the oldVersion to something high, round and imaginary.
  1. 为方便起见,将 oldVersion 的最后一部分更改为高、圆和假想的。

web.config assembly redirect with updated oldVersion

web.config assembly redirect with updated oldVersion

Rejoice.

麾。

回答by Sherlock-jr

Remember that with the binding redirection

请记住,使用绑定重定向

oldVersion="0.0.0.0-6.0.0.0"

oldVersion="0.0.0.0-6.0.0.0"

You are saying that the old versions of the dll are between version 0.0.0.0 and version 6.0.0.0.

你是说旧版本的dll在0.0.0.0和6.0.0.0版本之间。

回答by Veverke

No one mentioned the following, which in my understanding is the correct solution:

没有人提到以下内容,据我所知,这是正确的解决方案:

Go the csproj of the project where the nuget is installed, and set the AutoGEneratedBindingRedirectsto false.

去哪里安装的NuGet项目的csproj,并设置AutoGEneratedBindingRedirectsfalse

<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>

<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>

Full article in MSDN.

MSDN 中的全文。

回答by Adam Heeg

I updated my package and even reinstalled it - but I was still getting the exact same error as the OP mentioned. I manually edited the referenced dll by doing the following.

我更新了我的包,甚至重新安装了它 - 但我仍然遇到与 OP 提到的完全相同的错误。我通过执行以下操作手动编辑了引用的 dll。

I removed the newtonsoft.json.dll from my reference, then manually deleted the .dll from the bin directoy. Then i manually copied the newtonsoft.json.dll from the nuget package folder into the project bin, then added the reference by browsing to the .dll file.

我从我的参考中删除了 newtonsoft.json.dll,然后从 bin 目录中手动删除了 .dll。然后我手动将 nuget 包文件夹中的 newtonsoft.json.dll 复制到项目 bin 中,然后通过浏览到 .dll 文件添加引用。

Now my project builds again.

现在我的项目再次构建。

回答by Nick Niebling

I had similar issue and just wanted to post an answer for others in my situation.

我有类似的问题,只是想在我的情况下为其他人发布答案。

I have a solution running a ASP.NET Web Application with multiple other C# class lib projects.

我有一个运行 ASP.NET Web 应用程序和多个其他 C# 类库项目的解决方案。

My ASP.NET Web Application wasn't using json, but other projects where.

我的 ASP.NET Web 应用程序没有使用 json,而是使用了其他项目。

This is how I fixed it:

这是我修复它的方式:

  1. I made sure all projects where using latest version (6) using NuGet Update on all projects currently using any version of json - this didn't fix the issue
  2. I added json to the web application using NuGet - this fixed the issue (let me dive into why):
  1. 我确保所有使用最新版本 (6) 的项目在当前使用任何版本的 json 的所有项目上使用 NuGet 更新 - 这没有解决问题
  2. 我使用 NuGet 将 json 添加到 Web 应用程序 - 这解决了这个问题(让我深入了解原因):

Step 2 was first of all adding a configuration information for json, that suggest that all projects, use the latest version (6) no matter what version they have. Adding the assembly binding to Web.Config is most likely the fix.

第2步首先添加了json的配置信息,建议所有项目,无论是什么版本,都使用最新版本(6)。将程序集绑定添加到 Web.Config 很可能是修复。

However, step 2 also cleaned up som legacy code. It turned out we have previously used an old version (5) of json in our Web Application and the NuGet folders wasn't deleted when the reference was (I suspect: manually) removed. Adding the latest json (6), removed the old folders (json v5). This might be part of the fix as well.

但是,第 2 步也清理了一些遗留代码。事实证明,我们之前在我们的 Web 应用程序中使用了旧版本 (5) 的 json,并且当引用被(我怀疑:手动)删除时,NuGet 文件夹没有被删除。添加最新的json(6),删除旧文件夹(json v5)。这也可能是修复的一部分。