作法:啟用和停用自動繫結重新導向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. 按兩下警告,或選取警告,然後按 enter 鍵。Double-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