为什么 web.config 中的 bindingRedirects 在我的 Web 项目中不起作用?

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

提问人:Brondahl 提问时间:10/31/2023 最后编辑:Brondahl 更新时间:10/31/2023 访问量:43

问:

(IDE 是 Jetbrains Rider,如果相关)

设置:

  • 创建一个空白的“ASP.NET Web 应用程序”项目
  • 在 Nuget 中添加依赖项:
    • System.IdentityModel.Tokens.Jwt,版本=“4.0.4.403061554”。
    • Twilio,版本=“5.75.0”
  • 请注意,安装 Twilio 已将 Jwt 依赖项更新为 5.1.2.0,并在 和 中手动将其还原回 。packages.config.csproj
    • packages.config最终得到:
      • <package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net481" />
      • <package id="Twilio" version="5.75.0" targetFramework="net481" />
    • .csproj与这些版本匹配。

项目本身引用了 v 4.0.4.403061554 的 Jwt 库 Twilio 库(v 5.75)引用了 v 5.1.2.0 的 Jwt 库

正如预期的那样,强制项目具有对冲突版本的(传递)Nuget 引用会导致编译器警告,抱怨:

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

迄今。。。一切都如预期的那样。(大部分(*))

由于项目原因,我无法更改任何实际的版本依赖项以使其正常工作,但我也知道代码确实运行了;即使在运行时,警告也会表现为实际错误。

然而。。。警告很吵,我以为我知道如何解决这个问题。我的印象是这就是目的。bindingRedirects

因此,我已将其添加到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>

但这根本没有区别;编译器报告完全相同的错误。

但是,如果我将整个事情作为“ClassLibrary”项目进行,并将重定向添加到....然后突然 bindingRedirect “起作用”,编译器被安抚。app.config

什么给了?

为什么我的 web.config 中的 bindingRedirect 没有静默我收到的错误?

为什么它适用于 Library+app.config,但不适用于 WebProject+web.config?


(*) 眼尖的人可能会发现 4.0.40306.1554 与 4.0.4.403061554 的一些奇怪之处。这似乎是 Nuget 安装它的方式。当 Twilio 冲突不存在时,它工作正常,所以我假设那位在某种程度上都是犹太洁食。我尝试在任何地方使用完全相同的版本字符串,它只是以不同的方式破坏了它。当我执行 ClassLibrary 版本时,再次存在这种差异,此时 bindingRedirect 按预期工作。

c# .net-assembly 编译器-warnings 程序集绑定重定向

评论

1赞 Trisped 10/31/2023
随意将您的评论移到问题中,因为它们似乎是相关的。
0赞 Dai 10/31/2023
将 WebForms 转换为使用并完全回避此问题(右键单击“解决方案资源管理器”中的“引用”节点,然后选择“迁移到 PackageReference”)。.csproj<PackageReference>

答: 暂无答案