執行階段如何找出組件How the Runtime Locates Assemblies

若要成功部署 .NET Framework 應用程式,您必須了解 Common Language Runtime 如何找出並繫結至構成應用程式的組件。To successfully deploy your .NET Framework application, you must understand how the common language runtime locates and binds to the assemblies that make up your application. 根據預設,執行階段會嘗試與用來建置應用程式的組件正確版本繫結。By default, the runtime attempts to bind with the exact version of an assembly that the application was built with. 組態檔設定可覆寫這個預設行為。This default behavior can be overridden by configuration file settings.

在嘗試找出組件,並解析組件參考時,Common Language Runtime 會執行數個步驟。The common language runtime performs a number of steps when attempting to locate an assembly and resolve an assembly reference. 以下各節將會說明每個步驟。Each step is explained in the following sections. 在說明執行階段如何找出組件時,常會使用詞彙探查;它會依據其名稱和文化特性,參考用來尋找組件的啟發學習法集合。The term probing is often used when describing how the runtime locates assemblies; it refers to the set of heuristics used to locate the assembly based on its name and culture.

注意

您可以使用 Windows SDK 中隨附的組件繫結記錄檢視器 (Fuslogvw.exe) 來檢視記錄檔中的繫結資訊。You can view binding information in the log file using the Assembly Binding Log Viewer (Fuslogvw.exe), which is included in the Windows SDK.

初始化繫結Initiating the Bind

當執行階段嘗試將參考解析成另一個組件時,尋找並繫結至組件的程序即開始。The process of locating and binding to an assembly begins when the runtime attempts to resolve a reference to another assembly. 此參考可以是靜態或動態。This reference can be either static or dynamic. 編譯器會在建置時,將靜態參考記錄在組件資訊清單的中繼資料中。The compiler records static references in the assembly manifest's metadata at build time. 動態參考則是呼叫各種方法 (例如 Assembly.Load) 時,即時建構的結果。Dynamic references are constructed on the fly as a result of calling various methods, such as Assembly.Load.

參考組件時,最好是使用完整參考,包括組件名稱、版本、文化特性和公開金鑰語彙基元 (如果有的話)。The preferred way to reference an assembly is to use a full reference, including the assembly name, version, culture, and public key token (if one exists). 執行階段會使用此資訊來尋找組件 (依照本節稍後說明的步驟)。The runtime uses this information to locate the assembly, following the steps described later in this section. 不論是靜態或動態組件的參考,執行階段都會使用相同的解析程序。The runtime uses the same resolution process regardless of whether the reference is for a static or dynamic assembly.

您也可以只提供組件的部分資訊給呼叫方法 (例如只指定組件名稱),以對組件進行動態參考。You can also make a dynamic reference to an assembly by providing the calling method with only partial information about the assembly, such as specifying only the assembly name. 在此情況下,只會搜尋組件的應用程式目錄,而不會進行任何其他檢查。In this case, only the application directory is searched for the assembly, and no other checking occurs. 您可以使用 Assembly.LoadAppDomain.Load等各種方法來載入組件,以進行部分參考。You make a partial reference using any of the various methods for loading assemblies such as Assembly.Load or AppDomain.Load.

最後,您可使用 Assembly.Load 等方法建立動態參考,並且只提供部分資訊;然後使用應用程式組態檔中的 <qualifyAssembly> 元素授與參考資格。Finally, you can make a dynamic reference using a method such as Assembly.Load and provide only partial information; you then qualify the reference using the <qualifyAssembly> element in the application configuration file. 這個項目可讓您在應用程式組態檔中,而不是在您的程式碼中,提供完整的參考資訊 (名稱、版本、文化特性和 (適用的話) 公開金鑰語彙基元)。This element allows you to provide the full reference information (name, version, culture and, if applicable, the public key token) in your application configuration file instead of in your code. 如果您想要完整限定參考外部應用程式目錄中的組件,或者如果您想要參考全域組件快取中的組件,但又想要在組態檔中 (而不是在您的程式碼中) 指定完整參考的方便性,您就會使用這項技術。You would use this technique if you wanted to fully qualify a reference to an assembly outside the application directory, or if you wanted to reference an assembly in the global assembly cache but you wanted the convenience of specifying the full reference in the configuration file instead of in your code.

注意

這種類型的部分參考不應該用於數個應用程式之間共用的組件。This type of partial reference should not be used with assemblies that are shared among several applications. 因為組態設定是依各應用程式來套用,而且依各組件,所以使用這種部分參考的共用組件,會要求使用共用組件的每個應用程式,在其組態檔中都要有合格的資訊。Because configuration settings are applied per application and not per assembly, a shared assembly using this type of partial reference would require each application using the shared assembly to have the qualifying information in its configuration file.

執行階段會使用下列步驟來解析組件參考:The runtime uses the following steps to resolve an assembly reference:

  1. 檢查適用的組態檔,包括應用程式組態檔、發行者原則檔和電腦組態檔,以判斷正確的組件版本Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. 如果組態檔位於遠端電腦上,執行階段必須先找出並下載應用程式組態檔。If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.

  2. 檢查組件名稱之前是否已經被繫結 ,如果是的話,會使用先前載入的組件。Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly. 如果先前載入組件的要求失敗,此要求會立即失敗,而不會嘗試載入組件。If a previous request to load the assembly failed, the request is failed immediately without attempting to load the assembly.

    注意

    快取組件繫結失敗是 .NET Framework 2.0 版的新功能。The caching of assembly binding failures is new in the .NET Framework version 2.0.

  3. 檢查全域組件快取Checks the global assembly cache. 如果那裡找到組件,執行階段會使用此組件。If the assembly is found there, the runtime uses this assembly.

  4. 使用下列步驟來探查組件Probes for the assembly using the following steps:

    1. 如果組態和發行者原則不會影響原始參考,而且如果繫結要求是使用 Assembly.LoadFrom 方法來建立的,執行階段會檢查位置提示。If configuration and publisher policy do not affect the original reference and if the bind request was created using the Assembly.LoadFrom method, the runtime checks for location hints.

    2. 如果在組態檔中找到程式碼基底,執行階段只會檢查這個位置。If a codebase is found in the configuration files, the runtime checks only this location. 如果此探查失敗,執行階段會判定繫結要求失敗,且不會再發生其他探查。If this probe fails, the runtime determines that the binding request failed and no other probing occurs.

    3. 探查一節中會說明如何使用啟發學習法來探查組件。Probes for the assembly using the heuristics described in the probing section. 如果在探查之後找不到組件,執行階段會要求 Windows Installer 提供組件。If the assembly is not found after probing, the runtime requests the Windows Installer to provide the assembly. 這可以做為隨選安裝功能。This acts as an install-on-demand feature.

      注意

      針對沒有強式名稱的組件,不會進行版本檢查,執行階段也不會在全域組件快取中檢查沒有強式名稱的組件。There is no version checking for assemblies without strong names, nor does the runtime check in the global assembly cache for assemblies without strong names.

步驟 1:檢查組態檔Step 1: Examining the Configuration Files

根據下列三個 XML 檔案,可以在不同層級設定組件繫結行為:Assembly binding behavior can be configured at different levels based on three XML files:

  • 應用程式組態檔。Application configuration file.

  • 發行者原則檔。Publisher policy file.

  • 電腦組態檔。Machine configuration file.

這些檔案遵循相同的語法,並提供繫結重新導向、程式碼位置,以及特定組件的繫結模式等資訊。These files follow the same syntax and provide information such as binding redirects, the location of code, and binding modes for particular assemblies. 每個組態檔都可以包含重新導向繫結處理序的 <assemblyBinding> 項目Each configuration file can contain an <assemblyBinding> element that redirects the binding process. <assemblyBinding> 項目的子項目包括 <dependentAssembly> 項目The child elements of the <assemblyBinding> element include the <dependentAssembly> element. <dependentAssembly> 項目的子項目包括 <assemblyIdentity> 項目<bindingRedirect> 項目<codeBase> 項目The children of <dependentAssembly> element include the <assemblyIdentity> element, the <bindingRedirect> element, and the <codeBase> element.

注意

您可以在這三個組態檔中找到組態資訊;並非所有組態檔中的所有項目都有效。Configuration information can be found in the three configuration files; not all elements are valid in all configuration files. 例如,繫結模式和私用路徑資訊只能在應用程式組態檔中。For example, binding mode and private path information can only be in the application configuration file. 如需每個檔案內含資訊的完整清單,請參閱 使用組態檔設定應用程式For a complete list of the information that is contained in each file, see Configuring Apps by Using Configuration Files.

應用程式組態檔Application Configuration File

第一,Common Language Runtime 會檢查應用程式組態檔中,是否有資訊會覆寫儲存在呼叫組件資訊清單中的版本資訊。First, the common language runtime checks the application configuration file for information that overrides the version information stored in the calling assembly's manifest. 應用程式組態檔可以隨著應用程式一起部署,但並不是執行應用程式的必要項目。The application configuration file can be deployed with an application, but is not required for application execution. 通常擷取這個檔案幾乎是瞬間完成,但是如果應用程式基底是在遠端電腦上 (例如在 Internet Explorer Web 架構案例中),就必須下載組態檔。Usually the retrieval of this file is almost instantaneous, but in situations where the application base is on a remote computer, such as in an Internet Explorer Web-based scenario, the configuration file must be downloaded.

針對用戶端可執行檔,應用程式組態檔會位在與應用程式可執行檔相同的目錄中,而且基底名稱與可執行檔相同,副檔名為 .config。For client executables, the application configuration file resides in the same directory as the application's executable and has the same base name as the executable with a .config extension. 例如,C:\Program Files\Myapp\Myapp.exe 的組態檔為 C:\Program Files\Myapp\Myapp.exe.config。在以瀏覽器為主的案例中,HTML 檔必須使用 <link> 項目明確指向組態檔。For example, the configuration file for C:\Program Files\Myapp\Myapp.exe is C:\Program Files\Myapp\Myapp.exe.config. In a browser-based scenario, the HTML file must use the <link> element to explicitly point to the configuration file.

下列程式碼提供應用程式組態檔的簡單範例。The following code provides a simple example of an application configuration file. 這個範例會將 TextWriterTraceListener 加入 Listeners 集合,以啟用將偵錯資訊記錄至檔案的功能。This example adds a TextWriterTraceListener to the Listeners collection to enable recording debug information to a file.

<configuration>
   <system.diagnostics>
      <trace useGlobalLock="false" autoflush="true" indentsize="0">
         <listeners>
            <add name="myListener" type="System.Diagnostics.TextWriterTraceListener, system version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\myListener.log" />
         </listeners>
      </trace>
   </system.diagnostics>
</configuration>

發行者原則檔Publisher Policy File

第二,執行階段會檢查發行者原則檔 (如果有的話)。Second, the runtime examines the publisher policy file, if one exists. 元件發行者會將發行者原則檔以修正或更新形式發佈至共用元件。Publisher policy files are distributed by a component publisher as a fix or update to a shared component. 這些檔案包含共用元件發行者所發行的相容性資訊,可將組件參考導向至新版本。These files contain compatibility information issued by the publisher of the shared component that directs an assembly reference to a new version. 不同於應用程式和電腦組態檔,發行者原則檔是包含在自己的組件中,必須安裝在全域組件快取中。Unlike application and machine configuration files, publisher policy files are contained in their own assembly that must be installed in the global assembly cache.

發行者原則組態檔的範例如下:The following is an example of a Publisher Policy configuration file:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

            <dependentAssembly>
                <assemblyIdentity name="asm6" publicKeyToken="c0305c36380ba429" />
                <bindingRedirect oldVersion="3.0.0.0" newVersion="2.0.0.0"/>
            </dependentAssembly>

        </assemblyBinding>
    </runtime>
</configuration>

若要建立組件,您可以使用組件連結器 (Al.exe) 工具來搭配如下命令:To create an assembly, you can use the Al.exe (Assembly Linker) tool with a command such as the following:

Al.exe /link:asm6.exe.config /out:policy.3.0.asm6.dll /keyfile: compatkey.dat /v:3.0.0.0

compatkey.dat 是強式名稱金鑰檔案。compatkey.dat is a strong-name key file. 此命令會建立可以放在全域組件快取中的強式名稱組件。This command creates a strong-named assembly you can place in the global assembly cache.

注意

發行者原則會影響使用共用元件的所有應用程式。Publisher policy affects all applications that use a shared component.

發行者原則組態檔會覆寫來自應用程式的版本資訊 (也就是來自組件資訊清單,或來自應用程式組態檔)。The publisher policy configuration file overrides version information that comes from the application (that is, from the assembly manifest or from the application configuration file). 如果應用程式組態檔中沒有陳述式來將組件資訊清單中指定的版本重新導向,發行者原則檔就會覆寫組件資訊清單中指定的版本。If there is no statement in the application configuration file to redirect the version specified in the assembly manifest, the publisher policy file overrides the version specified in the assembly manifest. 不過,如果應用程式組態檔中有重新導向陳述式,發行者原則會覆寫該版本,而不是資訊清單中指定的版本。However, if there is a redirecting statement in the application configuration file, publisher policy overrides that version rather than the one specified in the manifest.

當共用元件更新時,就會使用發行者原則檔,而且使用該元件的所有應用程式,都應該收取共用元件的新版本。A publisher policy file is used when a shared component is updated and the new version of the shared component should be picked up by all applications using that component. 除非應用程式組態檔強制執行安全模式,否則發行者原則檔中的設定會覆寫應用程式組態檔中的設定。The settings in the publisher policy file override settings in the application configuration file, unless the application configuration file enforces safe mode.

安全模式Safe Mode

發行者原則檔通常會做為 Service Pack 或程式更新的一部分明確安裝。Publisher policy files are usually explicitly installed as part of a service pack or program update. 如果升級後的共用元件有任何問題,您可以使用安全模式來忽略發行者原則檔中的覆寫。If there is any problem with the upgraded shared component, you can ignore the overrides in the publisher policy file using safe mode. <publisherPolicy apply="yes|no"/> 項目決定安全模式,只能夠在應用程式組態檔中找到。Safe mode is determined by the <publisherPolicy apply="yes|no"/> element, located only in the application configuration file. 它會指定是否應該將發行者原則組態資訊從繫結程序中移除。It specifies whether the publisher policy configuration information should be removed from the binding process.

您可以針對整個應用程式或選取的組件來設定安全模式。Safe mode can be set for the entire application or for selected assemblies. 亦即,您可以針對構成應用程式的所有組件,關閉此原則,或是針對某些組件開啟此原則,但其他組件不開啟此原則。That is, you can turn off the policy for all assemblies that make up the application, or turn it on for some assemblies but not others. 若要選擇性地將發行者原則套用至構成應用程式的組件,請設定 <publisherPolicy apply=no/> ,並使用 <dependentAssembly> 項目指定要受影響的組件。To selectively apply publisher policy to assemblies that make up an application, set <publisherPolicy apply=no/> and specify which assemblies you want to be affected using the <dependentAssembly> element. 若要將發行者原則套用至構成應用程式的所有組件,請將 <publisherPolicy apply=no/> 設定為沒有相依組件項目。To apply publisher policy to all assemblies that make up the application, set <publisherPolicy apply=no/> with no dependent assembly elements. 如需組態的詳細資訊,請參閱 使用組態檔設定應用程式For more about configuration, see Configuring Apps by using Configuration Files.

電腦組態檔Machine Configuration File

第三,執行階段會檢查電腦組態檔。Third, the runtime examines the machine configuration file. 此檔案名為 Machine.config,位在本機電腦上,執行階段安裝所在之根目錄的 Config 子目錄中。This file, called Machine.config, resides on the local computer in the Config subdirectory of the root directory where the runtime is installed. 系統管理員可以使用這個檔案來指定電腦本機的組件繫結限制。This file can be used by administrators to specify assembly binding restrictions that are local to that computer. 電腦組態檔中的設定優先順序高於所有其他組態設定;不過,這不表示所有組態設定都應該放在這個檔案中。The settings in the machine configuration file take precedence over all other configuration settings; however, this does not mean that all configuration settings should be put in this file. 系統管理員原則檔決定的版本為最終版本,不能覆寫。The version determined by the administrator policy file is final, and cannot be overridden. 在 Machine.config 檔案中指定覆寫會影響所有應用程式。Overrides specified in the Machine.config file affect all applications. 如需組態檔的詳細資訊,請參閱 使用組態檔設定應用程式For more information about configuration files, see Configuring Apps by using Configuration Files.

步驟 2:檢查先前參考的組件Step 2: Checking for Previously Referenced Assemblies

如果先前的呼叫中也有要求現在所要求的組件,Common Language Runtime 會使用已載入的組件。If the requested assembly has also been requested in previous calls, the common language runtime uses the assembly that is already loaded. 在為構成應用程式的組件命名時,這可能會有分歧。This can have ramifications when naming assemblies that make up an application. 如需有關為組件命名的詳細資訊,請參閱 組件名稱For more information about naming assemblies, see Assembly Names.

如果先前要求組件失敗,後續要求該組件會立即失敗,而不會嘗試載入組件。If a previous request for the assembly failed, subsequent requests for the assembly are failed immediately without attempting to load the assembly. 從 .NET Framework 2.0 版開始,會快取組件繫結失敗,而所快取的資訊會用來判斷是否要嘗試載入組件。Starting with the .NET Framework version 2.0, assembly binding failures are cached, and the cached information is used to determine whether to attempt to load the assembly.

注意

若要還原至 .NET framework 1.0 和 1.1 版的行為 (不會快取繫結失敗),請將 <disableCachingBindingFailures> 項目包含在組態檔中。To revert to the behavior of the .NET Framework versions 1.0 and 1.1, which did not cache binding failures, include the <disableCachingBindingFailures> Element in your configuration file.

步驟 3:檢查全域組件快取Step 3: Checking the Global Assembly Cache

針對強式名稱組件,繫結程序會繼續在全域組件快取中查看。For strong-named assemblies, the binding process continues by looking in the global assembly cache. 全域組件快取會儲存可供電腦上數個應用程式使用的組件。The global assembly cache stores assemblies that can be used by several applications on a computer. 全域組件快取中的所有組件都必須具有強式名稱。All assemblies in the global assembly cache must have strong names.

步驟 4:透過程式碼基底或探查找出組件Step 4: Locating the Assembly through Codebases or Probing

使用呼叫組件參考和組態檔中的資訊來判斷正確的組件版本之後,以及在其簽入全域組件快取 (僅適用於強式名稱組件) 之後,Common Language Runtime 會嘗試尋找組件。After the correct assembly version has been determined by using the information in the calling assembly's reference and in the configuration files, and after it has checked in the global assembly cache (only for strong-named assemblies), the common language runtime attempts to find the assembly. 尋找組件的程序包含下列步驟:The process of locating an assembly involves the following steps:

  1. 如果在應用程式組態檔中找到 <codeBase> 項目,執行階段會檢查指定的位置。If a <codeBase> element is found in the application configuration file, the runtime checks the specified location. 如果找到相符項目,則會使用該組件,而不會進行探查。If a match is found, that assembly is used and no probing occurs. 如果在那裡沒有找到該組件,則繫結要求會失敗。If the assembly is not found there, the binding request fails.

  2. 然後執行階段會使用本節稍後指定的規則來探查參考的組件。The runtime then probes for the referenced assembly using the rules specified later in this section.

注意

如果目錄中有多個不同版本的組件,而且要參考特定版本的組件,就必須使用 <codeBase> 項目,而不能使用 <probing> 項目的 privatePath 屬性。If you have multiple versions of an assembly in a directory and you want to reference a particular version of that assembly, you must use the <codeBase> element instead of the privatePath attribute of the <probing> element. 如果使用 <probing> 項目,只要執行階段找到符合所參考之簡單組件名稱的組件 (無論是否確實相符),就會停止探查。If you use the <probing> element, the runtime stops probing the first time it finds an assembly that matches the simple assembly name referenced, whether it is a correct match or not. 如果是正確的相符項目,就會使用該組件。If it is a correct match, that assembly is used. 如果不是正確的相符項目,就會停止探查,且繫結失敗。If it is not a correct match, probing stops and binding fails.

透過程式碼基底找出組件Locating the Assembly through Codebases

您可以使用組態檔中的 <codeBase> 項目提供程式碼基底資訊。Codebase information can be provided by using a <codeBase> element in a configuration file. 在執行階段嘗試探查參考的組件之前,一定會先檢查此程式碼基底。This codebase is always checked before the runtime attempts to probe for the referenced assembly. 如果含有最終版本重新導向的發行者原則檔也包含 <codeBase> 項目,該 <codeBase> 項目就是所使用的項目。If a publisher policy file containing the final version redirect also contains a <codeBase> element, that <codeBase> element is the one that is used. 例如,如果您的應用程式組態檔指定 <codeBase> 項目,而覆寫應用程式資訊的發行者原則檔也指定 <codeBase> 項目,則會使用發行者原則檔中的 <codeBase> 項目。For example, if your application configuration file specifies a <codeBase> element, and a publisher policy file that is overriding the application information also specifies a <codeBase> element, the <codeBase> element in the publisher policy file is used.

如果在 <codeBase> 項目指定的位置中沒有找到符合的組件,繫結要求將失敗,並且不會採取進一步的步驟。If no match is found at the location specified by the <codeBase> element, the bind request fails and no further steps are taken. 如果執行階段判斷有組件符合呼叫組件的準則,就會使用該組件。If the runtime determines that an assembly matches the calling assembly's criteria, it uses that assembly. 載入由特定 <codeBase> 項目所指定的檔案時,執行階段會進行檢查,以確定名稱、版本、文化特性和公開金鑰與呼叫組件的參考相符。When the file specified by the given <codeBase> element is loaded, the runtime checks to make sure that the name, version, culture, and public key match the calling assembly's reference.

注意

應用程式根目錄之外的參考組件必須具有強式名稱,並且必須安裝於全域組件快取中,或者使用 <codeBase> 項目指定。Referenced assemblies outside the application's root directory must have strong names and must either be installed in the global assembly cache or specified using the <codeBase> element.

透過探查找出組件Locating the Assembly through Probing

如果應用程式組態檔中沒有 <codeBase> 項目,則執行階段會使用四個準則來探查組件:If there is no <codeBase> element in the application configuration file, the runtime probes for the assembly using four criteria:

  • 應用程式基底,這是執行應用程式所在的根位置。Application base, which is the root location where the application is being executed.

  • 文化特性,這是所參考之組件的文化特性屬性。Culture, which is the culture attribute of the assembly being referenced.

  • 名稱,這是所參考之組件的名稱。Name, which is the name of the referenced assembly.

  • <probing> 項目的 privatePath 屬性,這是根位置下使用者定義的子目錄清單。The privatePath attribute of the <probing> element, which is the user-defined list of subdirectories under the root location. 您可以使用應用程式定義域的 AppDomainSetup.PrivateBinPath 屬性,將這個位置指定在應用程式組態檔和 Managed 程式碼中。This location can be specified in the application configuration file and in managed code using the AppDomainSetup.PrivateBinPath property for an application domain. 如果指定在 Managed 程式碼中,會先探查 Managed 程式碼 privatePath ,後面接著應用程式組態檔中指定的路徑。When specified in managed code, the managed code privatePath is probed first, followed by the path specified in the application configuration file.

探查應用程式基底和文化特性目錄Probing the Application Base and Culture Directories

執行階段一定會從應用程式的基底開始探查,該基底可以是 URL,或是應用程式在電腦上的根目錄。The runtime always begins probing in the application's base, which can be either a URL or the application's root directory on a computer. 如果在應用程式基底中找不到參考的組件,而且沒有提供任何文化特性資訊,執行階段會搜尋具有該組件名稱的任何子目錄。If the referenced assembly is not found in the application base and no culture information is provided, the runtime searches any subdirectories with the assembly name. 所探查的目錄包括:The directories probed include:

  • [應用程式基底] / [組件名稱].dll[application base] / [assembly name].dll

  • [應用程式基底] / [組件名稱] / [組件名稱].dll[application base] / [assembly name] / [assembly name].dll

如果有為參考的組件指定文化特性資訊,就只會探查下列目錄:If culture information is specified for the referenced assembly, only the following directories are probed:

  • [應用程式基底] / [文化特性] / [組件名稱].dll[application base] / [culture] / [assembly name].dll

  • [應用程式基底] / [文化特性] / [組件名稱] / [組件名稱].dll[application base] / [culture] / [assembly name] / [assembly name].dll

使用 privatePath 屬性進行探查Probing with the privatePath Attribute

除了文化特性子目錄以及為參考組件所命名的子目錄以外,執行階段也會探查使用 <probing> 項目的 privatePath 屬性指定的目錄。In addition to the culture subdirectories and the subdirectories named for the referenced assembly, the runtime also probes directories specified using the privatePath attribute of the <probing> element. 使用 privatePath 屬性來指定的目錄,必須是應用程式根目錄的子目錄。The directories specified using the privatePath attribute must be subdirectories of the application's root directory. 所探查的目錄會因為文化特性資訊是否包含在參考的組件要求中而有所不同。The directories probed vary depending on whether culture information is included in the referenced assembly request.

執行階段只要一找到符合所參考之簡單組件名稱的組件 (無論是否確實相符),就會停止探查。The runtime stops probing the first time it finds an assembly that matches the simple assembly name referenced, whether it is a correct match or not. 如果是正確的相符項目,就會使用該組件。If it is a correct match, that assembly is used. 如果不是正確的相符項目,就會停止探查,且繫結失敗。If it is not a correct match, probing stops and binding fails.

如果包含文化特性,就會探查下列目錄:If culture is included, the following directories are probed:

  • [應用程式基底] / [bin 路徑] / [文化特性] / [組件名稱].dll[application base] / [binpath] / [culture] / [assembly name].dll

  • [應用程式基底] / [bin 路徑] / [文化特性] / [組件名稱] / [組件名稱].dll[application base] / [binpath] / [culture] / [assembly name] / [assembly name].dll

如果沒有包含文化特性資訊,就會探查下列目錄:If culture information is not included, the following directories are probed:

  • [應用程式基底] / [bin 路徑] / [組件名稱].dll[application base] / [binpath] / [assembly name].dll

  • [應用程式基底] / [bin 路徑] / [組件名稱] / [組件名稱].dll[application base] / [binpath] / [assembly name] / [assembly name].dll

探查範例Probing Examples

假設有下列資訊:Given the following information:

  • 參考的組件名稱:myAssemblyReferenced assembly name: myAssembly

  • 應用程式根目錄:http://www.code.microsoft.comApplication root directory: http://www.code.microsoft.com

  • 組態檔中的 <probing> 項目指定:bin<probing> element in configuration file specifies: bin

  • 文化特性:deCulture: de

執行階段會探查下列 URL:The runtime probes the following URLs:

  • http://www.code.microsoft.com/de/myAssembly.dll

  • http://www.code.microsoft.com/de/myAssembly/myAssembly.dll

  • http://www.code.microsoft.com/bin/de/myAssembly.dll

  • http://www.code.microsoft.com/bin/de/myAssembly/myAssembly.dll

具有相同名稱的多個組件Multiple Assemblies with the Same Name

下列範例示範如何設定具有相同名稱的多個組件。The following example shows how to configure multiple assemblies with the same name.

<dependentAssembly>
   <assemblyIdentity name="Server" publicKeyToken="c0305c36380ba429" />
   <codeBase version="1.0.0.0" href="v1/Server.dll" />
   <codeBase version="2.0.0.0" href="v2/Server.dll" />
</dependentAssembly>

探查的其他位置Other Locations Probed

組件位置也可以使用目前的繫結內容來決定。Assembly location can also be determined using the current binding context. 當使用 Assembly.LoadFrom 方法時,以及在 COM Interop 案例中,最常發生這個情況。This most often occurs when the Assembly.LoadFrom method is used and in COM interop scenarios. 如果組件使用 LoadFrom 方法來參考另一個組件,呼叫組件的位置會被視為有關在哪裡可以找到參考組件的提示。If an assembly uses the LoadFrom method to reference another assembly, the calling assembly's location is considered to be a hint about where to find the referenced assembly. 如果找到相符項目,就會載入該組件。If a match is found, that assembly is loaded. 如果沒有找到相符項目,執行階段會繼續進行其搜尋語意,然後查詢 Windows Installer,以提供組件。If no match is found, the runtime continues with its search semantics and then queries the Windows Installer to provide the assembly. 如果沒有提供符合繫結要求的組件,則會擲回例外狀況。If no assembly is provided that matches the binding request, an exception is thrown. 如果有參考類型,此例外狀況是 Managed 程式碼中的 TypeLoadException ,如果找不到要載入的組件,則為 FileNotFoundExceptionThis exception is a TypeLoadException in managed code if a type was referenced, or a FileNotFoundException if an assembly being loaded was not found.

比方說,如果 Assembly1 參考 Assembly2,而且 Assembly1 是從 http://www.code.microsoft.com/utils 下載,則該位置會被視為有關在哪裡可以找到 Assembly2.dll 的提示。For example, if Assembly1 references Assembly2 and Assembly1 was downloaded from http://www.code.microsoft.com/utils, that location is considered to be a hint about where to find Assembly2.dll. 執行階段接著會探查 http://www.code.microsoft.com/utils/Assembly2.dllhttp://www.code.microsoft.com/utils/Assembly2/Assembly2.dll 中的組件。The runtime then probes for the assembly in http://www.code.microsoft.com/utils/Assembly2.dll and http://www.code.microsoft.com/utils/Assembly2/Assembly2.dll. 如果在這兩個位置都找不到 Assembly2,執行階段就會查詢 Windows Installer。If Assembly2 is not found at either of those locations, the runtime queries the Windows Installer.

另請參閱See also