Search code examples
visual-studio-2017nugetassembly-binding-redirect

Nuget keeps reverting redirects to incorrect version, causing runtime errors


I have a solution where two different versions of a package are in use, call them "PackageA v1" and "PackageA v2".

One project references "PackageA v1" 15 projects reference "PackageA v2"

Every single time I do anything involving Nuget, all of the 15 projects have their app.config assembly binding redirect updated to redirect to the v1 package.

Before:

<dependentAssembly>
        <assemblyIdentity name="PackageA" publicKeyToken="abc123" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>

After:

<dependentAssembly>
    <assemblyIdentity name="PackageA" publicKeyToken="abc123" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
</dependentAssembly>

This happens even though the actual version that ends up in most of the project's bin folders is v2!

I tried removing v1 from the Nuget packages for the project referencing it, thinking that if I just copied v1's dll to the packages folder and added the reference manually that Nuget would not track v1 in whatever it uses to determine binding redirects. That didn't work.

Disabling automatic assembly binding redirection is not an option, since the solution has ~50 projects and (other than this) the automatic redirection saves quite a bit of time when upgrading packages.

Even something as simple as "make Nuget choose the newest version rather than the oldest" would be great. At least then I would only have to manually fix one app.config rather than 15.

I hope somebody out there has a solution. Thanks in advance!


Solution

  • Nuget keeps reverting redirects to incorrect version, causing runtime errors

    According to the bindingRedirect element on MSDN, I am afraid you could not do such things.

    oldVersion: Required attribute.

    Specifies the version of the assembly that was originally requested. The format of an assembly version number is major.minor.build.revision. Valid values for each part of this version number are 0 to 65535.

    You can also specify a range of versions in the following format: n.n.n.n - n.n.n.n

    newVersion: Required attribute. Specifies the version of the assembly to use instead of the originally requested version in the format: n.n.n.n

    This value can specify an earlier version than oldVersion.

    See this thread for some more details.

    But if you want to "something as simple as "make Nuget choose the newest version rather than the oldest" would be great", you could add the following attribute to your nuget.config(In the path, %appdata%\NuGet\NuGet.Config):

    <configuration>
        <config> 
            <add key="dependencyversion" value="Highest" /> 
        </config>
    </configuration>
    

    When resolving your packages, the latest version of that package will be resolved.