查找未使用/不必要的assemblyBinding重定向

23

看起来我们的 web.config 文件中有很多绑定重定向,我觉得它们要么:

  1. 看起来是不必要的
  2. 用于一些我没在我们的解决方案中看到被引用的程序集

这只是绑定重定向的一部分示例:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Azure.KeyVault.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
    <bindingRedirect oldVersion="10.0.0.0-11.0.0.0" newVersion="14.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.ApplicationInsights" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.5.1.0" newVersion="2.5.1.0" />
  </dependentAssembly>

我认为某个时刻Visual Studio决定自动添加许多绑定重定向。

是否有一种方法来验证是否需要任何绑定重定向或自动验证/删除它们?


1
它们很可能是其他程序集的依赖项。 - Knight King
2个回答

55

解决这个问题其实非常简单而优雅。

  1. 删除Web.config / app.config中所有的绑定重定向;
  2. 进入程序包管理器控制台
  3. 输入命令Add-BindingRedirect(你也可以使用-ProjectName "SpecificProject"指定目标项目);
  4. 所有必要的绑定重定向将被生成;
  5. 运行应用并检查是否正常工作。如果不正常,添加任何被该命令忽略的缺失的绑定重定向。

14
多项目解决方案中,使用Add-BindingRedirect *命令添加绑定重定向。 - hyankov
1
当我尝试这个时,命令会输出一堆东西,但是没有修改web.config或app.config文件。你必须手动浏览输出并逐个添加每个绑定重定向吗? - Mike Christensen
奇怪...也许它在更新的版本上出了问题,或者我的解决方案包含了太多的项目.. - Mike Christensen
@MikeChristensen 我也遇到过几次这种情况,直到我意识到我忘记在“程序包管理器控制台”窗口中选择默认项目。 - MMalke
1
今天最新的VS2022对我不起作用。唯一总是有效的方法是在项目文件中将GenerateBindingRedirectsOutputType设置为true,同时使用AutoGenerateBindingRedirects。然后在项目的bin文件夹中,我查找带有.config后缀的项目dll,所有重定向都会在那里,我必须将它们复制粘贴到我的web.config中。 - iBobb
显示剩余2条评论

6

大部分组件都是作为默认模板的一部分添加进来的。根据应用程序的需要,您可以安全地删除其中许多组件,在绑定和项目引用方面都可以如此操作。这样做的好处是,如果它们意外地被用作依赖项,您将立即得到通知。

例如:

  • "Microsoft.ApplicationInsights": 审计应用程序
  • System.Web.Helpers: 用于 MVC 的 Html 帮助程序
  • System.ValueTuple: 通过名称访问每个属性的元组数据结构
  • System.Threading.Tasks.Extensions: TPL 扩展方法
  • Microsoft.SqlServer.Types: 直接在应用程序代码中使用的 SQL Server 内注册的数据类型
  • Microsoft.Owin.Security: Owin 作为身份管理
  • Microsoft.Data.Edm: Entity Framework 数据建模
  • Microsoft.Data.OData: 开放式数据服务

请注意,当您的代码最初引用/请求旧版本并提供新版本时,Binding Redirect 是特别用于此目的。如果实际使用的版本与所提供的版本实际上相同(主要针对主要框架组件(而不是 NuGet 提供的更新),则可以完全删除 BindingRedirect 部分。

出于安全考虑,请注释掉每个部分,然后运行应用程序。如果出现问题,您可以取消注释该部分。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接