設定組件繫結重新導向Configuring Assembly Binding Redirection

根據預設,應用程式會使用執行階段版本 (用來編譯應用程式) 隨附的 .NET Framework 組件集。By default, applications use the set of .NET Framework assemblies that shipped with the runtime version used to compile the application. 您可以使用應用程式組態檔中 <assemblyBinding> 項目上的 appliesTo 屬性,來重新導向組件繫結參考至特定版本的 .NET Framework 組件。You can use the appliesTo attribute on the <assemblyBinding> element in an application configuration file to redirect assembly binding references to a specific version of the .NET Framework assemblies. 這個選擇性屬性會使用 .NET Framework 版本號碼,以表示它會套用至哪一個版本。This optional attribute uses a .NET Framework version number to indicate which version it applies to. 如果未指定 appliesTo 屬性,則 <assemblyBinding> 項目會套用至所有的 .NET Framework 版本。If no appliesTo attribute is specified, the <assemblyBinding> element applies to all versions of the .NET Framework.

在 .NET Framework 1.1 版中引進了 appliesTo 屬性;而 .NET Framework 1.0 版則會忽略它。The appliesTo attribute was introduced in the .NET Framework version 1.1; it is ignored by the .NET Framework version 1.0. 這表示在使用 .NET Framework 1.0 版時,會套用所有 <assemblyBinding> 項目,即使已指定 appliesTo 屬性時也是如此。This means that all <assemblyBinding> elements are applied when using the .NET Framework version 1.0, even if an appliesTo attribute is specified.

注意

使用 appliesTo 屬性來限制組件繫結重新導向至特定版本的執行階段。Use the appliesTo attribute to limit assembly binding redirection to a specific version of the runtime.

例如對於 .NET Framework 1.0 版組件,如果要重新導向組件繫結,您可在應用程式組態檔中包含下列 XML 程式碼。For example, to redirect assembly binding for a .NET Framework version 1.0 assembly, you would include the following XML code in your application configuration file.

<runtime>  
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">  
            <dependentAssembly>   
               * assembly information goes here *  
            </dependentAssembly>  
       </assemblyBinding>  
</runtime>  

<assemblyBinding> 項目要區分順序。The <assemblyBinding> elements are order-sensitive. 您應先輸入任何 .NET Framework 1.0 版組件的組件繫結重新導向資訊,然後才是任何 .NET Framework 1.1 版組件的組件繫結重新導向資訊。You should enter assembly binding redirection information for any .NET Framework version 1.0 assemblies first, followed by assembly binding redirection information for any .NET Framework version 1.1 assemblies. 最後,請輸入不使用 appliesTo 屬性的任何 .NET Framework 組件重新導向之組件繫結重新導向資訊,如此一來會適用於所有版本的 .NET Framework。Finally, enter assembly binding redirection information for any .NET Framework assembly redirection that does not use the appliesTo attribute and therefore applies to all versions of the .NET Framework. 若重新導向發生衝突,會使用組態檔中第一筆相符的重新導向陳述式。In case of a conflict in redirection, the first matching redirection statement in the configuration file is used.

例如,若要重新導向一個參考至 .NET Framework 1.0 版組件和另一個參考至 .NET Framework 1.1 版組件,則您可使用下列虛擬程式碼所示的模式。For example, to redirect one reference to a .NET Framework version 1.0 assembly and another reference to a .NET Framework version 1.1 assembly, you would use the pattern shown in the following pseudocode.

<assemblyBinding xmlns="..." appliesTo="v1.0.3705">   
  <!-- .NET Framework version 1.0 redirects here. -->   
</assemblyBinding>   
  
<assemblyBinding xmlns="..." appliesTo="v1.1.4322">   
  <!-- .NET Framework version 1.1 redirects here. -->   
</assemblyBinding>   
  
<assemblyBinding xmlns="...">   
  <!-- Redirects meant for all versions of the .NET Framework. -->   
</assemblyBinding>  

偵錯組態檔錯誤Debugging Configuration File Errors

一旦建立應用程式定義域,並載入程式碼至應用程式定義域中,執行階段就會剖析組態檔。The runtime parses configuration files once when an application domain is created, and loads code into that application domain. Common Language Runtime 會忽略此項目,以處理組態檔中的錯誤。The common language runtime handles errors in a configuration file by ignoring the entry. 如果它包含格式不正確的 XML,則執行階段會略過整個組態檔案。The runtime ignores the entire configuration file if it contains malformed XML. 對於無效的 XML,則會忽略無效的區段。For invalid XML, only the invalid sections are ignored.

您可判斷組態檔是否用於決定正在進行組件繫結重新導向。You can determine whether a configuration file is being used by determining whether assembly binding redirects are occurring. 使用組件繫結記錄檔檢視器 (Fuslogvw.exe) 來查看正在載入哪些組件。Use the Assembly Binding Log Viewer (Fuslogvw.exe) to see which assemblies are being loaded. 若要查看所有組件繫結,您必須在登錄中設定 ForceLog 項目。To see all assembly binds, you must set an entry for ForceLog in the registry.

另請參閱See also