Why aren't the bindingRedirects in web.config working in my Web Project?

82 Views Asked by At

(IDE is Jetbrains Rider, in case relevant)

Setup:

  • Create a blank "ASP.NET Web Application" project
  • Add dependencies in Nuget:
    • System.IdentityModel.Tokens.Jwt, version="4.0.4.403061554".
    • Twilio, version="5.75.0"
  • Observe that installing Twilio has updated the Jwt dependency to 5.1.2.0, and manually revert it back down in packages.config, and the .csproj.
    • packages.config ends up with:
      • <package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net481" />
      • <package id="Twilio" version="5.75.0" targetFramework="net481" />
    • .csproj matches those versions.

The project itself references the Jwt library at v 4.0.4.403061554 The Twilio library (at v 5.75) references the Jwt library at v 5.1.2.0

As expected forcing the project to have (transitive) Nuget references to conflicting versions leads to a compiler warning complaining:

Warning MSB3277 : Found conflicts between different versions of "System.IdentityModel.Tokens.Jwt" that could not be resolved.
There was a conflict between "System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and "System.IdentityModel.Tokens.Jwt, Version=5.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
    "System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35" was chosen because it was primary and "System.IdentityModel.Tokens.Jwt, Version=5.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" was not.
    References which depend on "System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35" [C:\Work\...\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll].
        C:\Work\...\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll
          Project file item includes which caused reference "C:\Work\...\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll".
            System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL
    References which depend on "System.IdentityModel.Tokens.Jwt, Version=5.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" [].
        C:\Work\...\packages\Twilio.5.75.0\lib\net451\Twilio.dll
          Project file item includes which caused reference "C:\Work\...\packages\Twilio.5.75.0\lib\net451\Twilio.dll".
            Twilio, Version=5.75.0.0, Culture=neutral, processorArchitecture=MSIL

So far ... all as expected. (Mostly(*))

For project reasons I can't change any of the actual version dependencies enough to make this all work nicely, but I also know that the code does actually run; the warnings are manifesting into actual errors, even at runtime.

However ... warnings are noisy, and I thought I knew how to fix this. I was under the impression that this is what bindingRedirects are for.

So I've added this to the web.config:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    ...
    <dependentAssembly>
      <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-5.1.2.0" newVersion="4.0.40306.1554" />
    </dependentAssembly>
    ...
  </assemblyBinding>
</runtime>

But it made no difference at all; the compiler reports exactly the same error.

However, if I do the whole thing as a "ClassLibrary" project, and add the redirect to the app.config instead .... then suddenly the bindingRedirect "works" and the compiler is placated.

What gives?

Why is the bindingRedirect in my web.config not silencing the error that I'm receiving?

Why does it work for Library+app.config, but not WebProject+web.config?


(*) The eagle-eyed may spot some weirdness with 4.0.40306.1554 vs 4.0.4.403061554. This appears to be the way that Nuget installs it. It works fine when the Twilio conflict isn't present, so I assume that bit is somehow all kosher. I've tried using the exact same version string everywhere for that and it just breaks it in different ways. And again that discrepancy is present when I do the ClassLibrary version, at which point the bindingRedirect works as expected.

0

There are 0 best solutions below