Nuget - 如何使用 configSource 和 Add-BindingRedirect

Nuget - How to use configSource and Add-BindingRedirect

有没有办法将 app.configassemblyBinding 元素移动到外部文件,并在 Visual Studio 的包管理器控制台中使用 Add-BindingRedirect 更新它?

这是我的外部文件:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

这是我的 app.config 引用文件的片段:

<runtime>
  <assemblyBinding configSource="runtime.config" />
</runtime>

这是包管理器控制台中 运行 Add-BindingRedirect 之后的 app.config 文件:

<runtime>
  <assemblyBinding configSource="runtime.config" />
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

我们无法在程序包管理器控制台中使用 Add-BindingRedirect 将元素添加到外部文件。因为 Add-BindingRedirect 曾经 检查项目输出路径中的所有程序集,并在必要时将 绑定重定向添加到 app.configweb.config .

A​​dd-BindingRedirect请参考定义: http://docs.nuget.org/ndocs/tools/powershell-reference

并且configSource属性与外部配置文件一起使用,为项目添加一些配置。也就是说,它们是两种不同的方式帮助我们实现绑定重定向。所以不能混用。