HOW TO:啟用和停用自動繫結重新導向How to: Enable and Disable Automatic Binding Redirection

當您編譯.NET Framework 4.5.1 為目標的 Visual Studio 和更新版本中的應用程式時,繫結重新導向可能會自動加入至應用程式組態檔覆寫組件統一。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. 自動繫結重新導向功能會影響傳統型應用程式和 web 應用程式目標為.NET Framework 4.5.1 或更新版本,但行為會稍有不同的 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. 同樣地,以滑鼠右鍵按一下已卸載的專案,然後選擇 [編輯 [projectname.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. 如果您較新的 framework 版本為目標,但不要收到自動提示加入重新導向,您可能會建議您重新對應組件的組建輸出。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. 同樣地,以滑鼠右鍵按一下已卸載的專案,然後選擇 [編輯 [projectname.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.config) 檔必須經過修改的 web 應用程式中,繫結重新導向不會自動新增至組態檔。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