ASP.NET Web项目中web.config的bindingRedirect为何不生效?
NuGet版本冲突与绑定重定向差异问题(IDE:JetBrains Rider)
环境配置
- 创建空白ASP.NET Web Application项目
- 通过NuGet添加以下依赖:
System.IdentityModel.Tokens.Jwt,版本4.0.4.403061554Twilio,版本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中对应版本与上述保持一致
版本冲突与编译器警告
项目直接引用4.0.40306.1554版本的Jwt库,而5.75.0版本的Twilio库依赖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
注:细心的人可能会注意到
4.0.40306.1554与4.0.4.403061554的差异,这是NuGet默认安装行为导致的。无Twilio冲突时项目可正常运行,因此认为该差异无实质影响;尝试统一版本字符串会引发其他问题,且类库项目中存在此差异时绑定重定向仍能正常工作。
到目前为止一切符合预期,但后续警告演变为运行时错误。由于项目限制无法调整版本依赖,尝试通过绑定重定向解决问题。
绑定重定向尝试及差异表现
在Web项目的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类库项目,并在app.config中添加相同的绑定重定向配置后,绑定重定向生效,编译器不再报版本冲突警告。
核心问题
- 为何Web项目中
web.config的绑定重定向无法消除版本冲突警告? - 为何类库项目+
app.config的配置有效,而Web项目+web.config无效?
内容的提问来源于stack exchange,提问作者Brondahl
相关产品推荐
相关产品推荐

