【问题标题】:Why is visual studio creating app.configs for assembly redirection?为什么 Visual Studio 为程序集重定向创建 app.configs?
【发布时间】:2018-12-12 05:54:51
【问题描述】:

我有一个包含大约 100 个项目的解决方案。我不知道究竟是什么触发了它,但 Visual Studio 偶尔会在我的每个项目中添加一个 app.config。它通常看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

据我所知,这些 app.config 文件是不需要的。如果我恢复所有这些更改并删除 app.config 文件,一切似乎都可以正常工作。为什么 Visual Studio 会这样做,我怎样才能让它停止?

【问题讨论】:

    标签: c# visual-studio visual-studio-2017 assembly-binding-redirect


    【解决方案1】:

    当你更新 nuget 包时,VS 通常会添加这些配置(绑定重定向)。它有助于防止 .NET 在查找依赖项时出现一些问题,例如

      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    

    在此配置中,我们讨论如果您的应用程序的某些其他依赖项引用了版本低于 11.0.0.0 的程序集,则它们应使用版本 11.0.0.0。在这种情况下,如果您忽略此绑定重定向,您将在运行时遇到异常。

    如果您的应用中没有此类依赖项,您可以删除这些配置。

    【讨论】:

    • 所以,理论上,如果我确保我的其他项目都没有引用低于 11.0.0.0 的版本,VS 应该停止创建 app.configs 并将它们包含到我的 .csproj 文件中?
    • 您可以确保您的其他项目都没有引用低于 11.0.0.0 的版本,但问题是您无法确保第三方程序集没有对以前版本的引用,如果他们有如果你省略这个,你会出错。但如果你确定他们不这样做,你可以安全地删除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2014-11-20
    • 2010-09-13
    相关资源
    最近更新 更多