简体   繁体   中英

Why am I in DotNet dependency hell and what can I do to get out?

I have a DotNet 4.6.1 application with MVC and WebAPI. The MVC side has GlobalConfiguration.Configuration and the WebAPI has a dependency on Assembly1

Apparently GlobalConfiguration.Configuration in System.Web.Http has a dependency on "Newtonsoft.Json, Version=6.0.0.0", and Assembly1 has a dependency on "Newtonsoft.Json", Version=7.0.1.

I placed those quotations precisely because these are precise dependencies: When I try to run a ping against my WebApi I get:

"Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies"

My web.config was added to by NuGet and created by Microsoft so until now I had not touched it and it was built for me. The structure of the web.config is:

<!--Personal Comment: See how configuration has no namespace-->
<configuration>
    <runtime>
        <!--Personal Comment: See how assemblyBinding DOES have namespace-->
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <!--Left out all of the other dependentAssemblies for brevity-->
            <dependentAssembly>
                <!--Personal Comment: Take note the upper/lowercase of attributes-->
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral">
                    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0 newVersion="7.0.0.0" />
                </assemblyIdentity>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

The odd thing is, if I go to the .csproj in VSCode and change the reference to Include="Newtonsoft.Json" and remove Version=7.0.0.0, etc... then nothing gets fixed. But if I change Version=7.0.0.0 to Version=6.0.0.0 and direct the hintpath to 7.0.1 then my solution works!

This seems like an awful way to live and to program and I don't like it unless I have to deal with it. According to every article I read online, every question, every answer, they all say "use bindingRedirect" and they use it in the way I tried. My assumption is that bindingRedirect is not working for my code, and I need to know why, or could I somehow reference Newtonsoft.Json twice in my references and tell the compiler to use the 7.0.0.0 code if it's not third-party project?

I experienced a similar issue a while ago. Try this:

  • Remove the Json dependency in any projects that use it.
  • Close the solution.
  • Delete the SUO file. It has the same name of your solution with a .SUO extension.
  • Relaunch the solution.
  • Clean the solution.
  • Add the desired Json dependency to the projects that need it.
  • Rebuild the solution.
  • Run to verify.

The reason is that Visual Studio appears to "cache" the references in the SUO file.

If it still misbehaves, set the compiler to verbose and rebuild. It should tell you the exact source of the mismatch in the output.

If all the dependencies are managed using NuGet, you can open Manage NuGet Packages for Solution and upgrade each project to the latest version of a package (NewtonSoft.JSON in this case). The older package version(s) will be uninstalled once it is not referenced any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM