Search code examples
c#dependenciesversion.net-standard

Library trying to load wrong version of assembly


I have a .NET Standard project that uses the library "RethinkDb.Driver" but when I start my project and I have the following error

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

However, my project already uses Newtonsoft.Json 12.0.3. I however can't go back to the version 10.0.0 because I'm also using another library that needs the latest version.
I checked my .csproj and the PackageReference is here, pointing to the version 12.0.3
I also already tried things like cleaning the NuGet cache but it didn't fix anything.


Solution

  • You have to use assembly redirect.

    You have to put it in your config file. It will be app.config in library and executable project, and web.config if it's a web project

    That is a directive to dotnet to resolve this dependency to newVersion

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-12.0.3.0" newVersion="12.0.3.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>