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

后端 未结 11 1601
粉色の甜心
粉色の甜心 2020-11-29 20:42

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:

<
相关标签:
11条回答
  • 2020-11-29 21:10

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

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

    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.

    0 讨论(0)
  • 2020-11-29 21:13

    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.

    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.

    Now my project builds again.

    0 讨论(0)
  • 2020-11-29 21:14

    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.

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

     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.

    I have a feeling this will come up again.

    0 讨论(0)
  • 2020-11-29 21:18

    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
    0 讨论(0)
  • 2020-11-29 21:20

    If none of the above works, try using this in web.config or 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>
    
    0 讨论(0)
  • 2020-11-29 21:27

    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>
    

    0 讨论(0)
提交回复
热议问题