如何:启用和禁用自动绑定重定向How to: Enable and Disable Automatic Binding Redirection

在 Visual Studio 中编译面向 .NET Framework 4.5.1 和更高版本的应用程序时, 绑定重定向可能会自动添加到应用程序配置文件中, 以替代程序集统一。When you compile apps in Visual Studio that target the .NET Framework 4.5.1 and later versions, binding redirects may be automatically added to the app configuration file to override assembly unification. 如果你的应用或其组件引用同一程序集的多个版本,就会添加绑定重定向,即使你在应用的配置文件中手动指定绑定重定向。Binding redirects are added if your app or its components reference more than one version of the same assembly, even if you manually specify binding redirects in the configuration file for your app. 自动绑定重定向功能会影响面向 .NET Framework 4.5.1 或更高版本的桌面应用和 web 应用, 但对于 web 应用而言, 此行为稍有不同。The automatic binding redirection feature affects desktop apps and web apps that target the .NET Framework 4.5.1 or a later version, although the behavior is slightly different for a web app. 如果现有应用面向 .NET Framework 的早期版本, 则可以启用自动绑定重定向, 如果要手动创作绑定重定向, 则可以禁用此功能。You can enable automatic binding redirection if you have existing apps that target previous versions of the .NET Framework, or you can disable this feature if you want to manually author binding redirects.

禁用桌面应用中的自动绑定重定向Disable automatic binding redirects in desktop apps

默认情况下, 为面向 .NET Framework 4.5.1 和更高版本的 Windows 桌面应用启用自动绑定重定向。Automatic binding redirects are enabled by default for Windows desktop apps that target the .NET Framework 4.5.1 and later versions. 在编译应用程序时, 绑定重定向将添加到输出配置 (app.config) 文件, 并重写可能发生的程序集统一。The binding redirects are added to the output configuration (app.config) file when the app is compiled and override the assembly unification that might otherwise take place. 未修改源app.config文件。The source app.config file is not modified. 可以通过修改应用程序的项目文件, 或在 Visual Studio 的项目属性中取消选中复选框来禁用此功能。You can disable this feature by modifying the project file for the app or by deselecting a checkbox in the project's properties in Visual Studio.

通过项目属性禁用Disable through project properties

如果使用的是 Visual Studio 2017 版本15.7 或更高版本, 则可以在项目的属性页中轻松禁用自动生成的绑定重定向。If you have Visual Studio 2017 version 15.7 or later, you can easily disable autogenerated binding redirects in the project's property pages.

  1. 右键单击“解决方案资源管理器”中的项目,再选择“属性”。Right-click the project in Solution Explorer and select Properties.

  2. 应用程序页上, 取消选中 "自动生成绑定重定向" 选项。On the Application page, uncheck the Auto-generate binding redirects option.

  3. Ctrl+S保存更改。Press Ctrl+S to save the change.

在项目文件中手动禁用Disable manually in the project file

  1. 使用以下方法之一打开项目文件以进行编辑:Open the project file for editing using one of the following methods:

    • 在 Visual Studio 中, 选择解决方案资源管理器中的项目, 然后从快捷菜单中选择 "在文件资源管理器中打开文件夹"。In Visual Studio, select the project in Solution Explorer, and then choose Open Folder in File Explorer from the shortcut menu. 在文件资源管理器中, 找到项目 (.csproj 或 .vbproj) 文件并在记事本中将其打开。In File Explorer, find the project (.csproj or .vbproj) file and open it in Notepad.
    • 在 Visual Studio 的解决方案资源管理器中, 右键单击项目, 然后选择 "卸载项目"。In Visual Studio, in Solution Explorer, right-click the project and choose Unload Project. 再次右键单击卸载的项目, 然后选择 "编辑 [项目名称 .csproj] "。Right-click the unloaded project again, and then choose Edit [projectname.csproj].
  2. 在项目文件中,查找以下属性项:In the project file, find the following property entry:

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    
  3. true 更改为 falseChange true to false:

    <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
    

手动启用自动绑定重定向Enable automatic binding redirects manually

你可以在面向旧版本 .NET Framework 的现有应用中启用自动绑定重定向, 或在不会自动提示你添加重定向的情况下启用自动绑定重定向。You can enable automatic binding redirects in existing apps that target older versions of the .NET Framework, or in cases where you're not automatically prompted to add a redirect. 如果针对的是较新版本的框架, 但不会自动提示添加重定向, 则可能会获得建议重新映射程序集的生成输出。If you're targeting a newer version of the framework but do not get automatically prompted to add a redirect, you'll likely get build output that suggests you remap assemblies.

  1. 使用以下方法之一打开项目文件以进行编辑:Open the project file for editing using one of the following methods:

    • 在 Visual Studio 中, 选择解决方案资源管理器中的项目, 然后从快捷菜单中选择 "在文件资源管理器中打开文件夹"。In Visual Studio, select the project in Solution Explorer, and then choose Open Folder in File Explorer from the shortcut menu. 在文件资源管理器中, 找到项目 (.csproj 或 .vbproj) 文件并在记事本中将其打开。In File Explorer, find the project (.csproj or .vbproj) file and open it in Notepad.
    • 在 Visual Studio 的解决方案资源管理器中, 右键单击项目, 然后选择 "卸载项目"。In Visual Studio, in Solution Explorer, right-click the project and choose Unload Project. 再次右键单击卸载的项目, 然后选择 "编辑 [项目名称 .csproj] "。Right-click the unloaded project again, and then choose Edit [projectname.csproj].
  2. 将以下元素添加到第一个配置属性组 (在<PropertyGroup > 标记下):Add the following element to the first configuration property group (under the <PropertyGroup> tag):

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    

    下面显示了一个示例项目文件, 其中插入了元素:The following shows an example project file with the element inserted:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{123334}</ProjectGuid>
        ...
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      </PropertyGroup>
      ...
    </Project>
    
  3. 编译你的应用。Compile your app.

在 web 应用中启用自动绑定重定向Enable automatic binding redirects in web apps

Web 应用的自动绑定重定向实现方式有所不同。Automatic binding redirects are implemented differently for web apps. 由于必须为 web 应用修改源配置 (web.config) 文件, 因此绑定重定向不会自动添加到配置文件中。Because the source configuration (web.config) file must be modified for web apps, binding redirects are not automatically added to the configuration file. 但是,Visual Studio 会通知你绑定冲突,你可以添加绑定重定向来解决此冲突。However, Visual Studio notifies you of binding conflicts, and you can add binding redirects to resolve the conflicts. 由于始终会提示你添加绑定重定向, 因此你不需要为 web 应用显式禁用此功能。Because you're always prompted to add binding redirects, you don't need to explicitly disable this feature for a web app.

向 web.config 文件添加绑定重定向:To add binding redirects to a web.config file:

  1. 在 Visual Studio 中,编译应用,然后检查生成警告。In Visual Studio, compile the app, and check for build warnings.

    程序集引用冲突的生成警告Build warning for assembly reference conflicts

  2. 如果存在程序集绑定冲突,则将显示警告。If there are assembly binding conflicts, a warning appears. 双击警告, 或选择警告, 然后按enterDouble-click the warning, or select the warning and press Enter.

    一个对话框, 使你能够自动将必要的绑定重定向添加到源web.config文件中。A dialog box that enables you to automatically add the necessary binding redirects to the source web.config file appears.

    绑定重定向权限对话框Binding redirect permission dialog

请参阅See also