陰影複製組件Shadow Copying Assemblies

陰影複製可讓應用程式定義域中使用的組件更新,而不需卸載應用程式定義域。Shadow copying enables assemblies that are used in an application domain to be updated without unloading the application domain. 這對必須連續運作的應用程式特別有用,例如 ASP.NET 網站。This is particularly useful for applications that must be available continuously, such as ASP.NET sites.

重要

Windows 8.x 市集Windows 8.x Store應用程式不支援陰影複製。Shadow copying is not supported in Windows 8.x 市集Windows 8.x Store apps.

Common Language Runtime 在載入組件時會鎖定組件檔案,因此在卸載組件之前無法更新檔案。The common language runtime locks an assembly file when the assembly is loaded, so the file cannot be updated until the assembly is unloaded. 若要從應用程式定義域卸載組件,唯一的方式為卸載應用程式定義域,如此在正常情況下,就無法在磁碟上更新組件,直到使用組件的所有應用程式定義域已卸載為止。The only way to unload an assembly from an application domain is by unloading the application domain, so under normal circumstances, an assembly cannot be updated on disk until all the application domains that are using it have been unloaded.

當設定應用程式定義域來陰影複製檔案時,會從應用程式路徑複製組件到另一個位置,並從該位置載入。When an application domain is configured to shadow copy files, assemblies from the application path are copied to another location and loaded from that location. 複本會遭到鎖定,但原始組件檔已解除鎖定,而且可以更新。The copy is locked, but the original assembly file is unlocked and can be updated.

重要

唯一可以進行陰影複製的組件是儲存在應用程式目錄或其子目錄的,在設定應用程式定義域時可由 ApplicationBasePrivateBinPath 屬性指定。The only assemblies that can be shadow copied are those stored in the application directory or its subdirectories, specified by the ApplicationBase and PrivateBinPath properties when the application domain is configured. 儲存在全域組件快取的組件不會進行陰影複製。Assemblies stored in the global assembly cache are not shadow copied.

本文包含下列章節:This article contains the following sections:

  • 啟用和使用陰影複製描述基本的用法,以及陰影複製可用的選項。Enabling and Using Shadow Copying describes the basic use and the options that are available for shadow copying.

  • 啟動效能描述為了在 .NET Framework 4 中陰影複製而進行的變更,以改善啟動效能,也描述如何還原為舊版的行為。Startup Performance describes the changes that are made to shadow copying in the .NET Framework 4 to improve startup performance, and how to revert to the behavior of earlier versions.

  • 已淘汰方法會描述對 .NET Framework 2.0 中控制陰影複製屬性和方法所進行的變更。Obsolete Methods describes the changes that were made to the properties and methods that control shadow copying in the .NET Framework 2.0.

啟用及使用陰影複製Enabling and Using Shadow Copying

您可以使用 AppDomainSetup 類別的屬性,如下所示,設定用於陰影複製的應用程式定義域:You can use the properties of the AppDomainSetup class as follows to configure an application domain for shadow copying:

  • ShadowCopyFiles 屬性設為字串值 "true" 來啟用陰影複製。Enable shadow copying by setting the ShadowCopyFiles property to the string value "true".

    根據預設,在載入組件之前,此設定會複製在應用程式路徑的所有組件到下載快取。By default, this setting causes all assemblies in the application path to be copied to a download cache before they are loaded. 這與 Common Language Runtime 所維護的快取相同,用以儲存從其他電腦下載的檔案,而且當這些檔案不再必需時,Common Language Runtime 會自動刪除這些檔案。This is the same cache maintained by the common language runtime to store files downloaded from other computers, and the common language runtime automatically deletes the files when they are no longer needed.

  • 使用 CachePath 屬性和 ApplicationName 屬性,選擇性地設定用於陰影複製檔案的自訂位置。Optionally set a custom location for shadow copied files by using the CachePath property and the ApplicationName property.

    藉由串連 ApplicationName 屬性至 CachePath 屬性做為子目錄,形成此位置的基底路徑。The base path for the location is formed by concatenating the ApplicationName property to the CachePath property as a subdirectory. 這會陰影複製組件到這個路徑的子目錄,而非本身的基底路徑。Assemblies are shadow copied to subdirectories of this path, not to the base path itself.

    注意

    如果未設定 ApplicationName 屬性,則會忽略 CachePath 屬性,並使用下載快取。If the ApplicationName property is not set, the CachePath property is ignored and the download cache is used. 不會有例外狀況擲回。No exception is thrown.

    如果您指定自訂位置,則當您不再需要檔案時,要負責清除目錄及複製檔案。If you specify a custom location, you are responsible for cleaning up the directories and copied files when they are no longer needed. 它們不會自動刪除。They are not deleted automatically.

    有幾個理由可能會讓您想設定陰影複製檔案的自訂位置。There are a few reasons why you might want to set a custom location for shadow copied files. 如果您的應用程式會產生大量的複本,則您可能會想要設定陰影複製檔案的自訂位置。You might want to set a custom location for shadow copied files if your application generates a large number of copies. 下載快取會受大小所限制,而非存留期,因此 Common Language Runtime 很可能會嘗試刪除仍在使用中的檔案。The download cache is limited by size, not by lifetime, so it is possible that the common language runtime will attempt to delete a file that is still in use. 設定自訂位置的另一個理由是執行您應用程式的使用者沒有寫入權限,來寫入 Common Language Runtime 為下載快取所使用的目錄位置。Another reason to set a custom location is when users running your application do not have write access to the directory location the common language runtime uses for the download cache.

  • 使用 ShadowCopyDirectories 屬性來選擇性地限制陰影複製的組件。Optionally limit the assemblies that are shadow copied by using the ShadowCopyDirectories property.

    當您啟用應用程式定義域的陰影複製時,預設會複製所有組件到應用程式路徑中;也就是 ApplicationBasePrivateBinPath 屬性所指定的目錄中。When you enable shadow copying for an application domain, the default is to copy all assemblies in the application path — that is, in the directories specified by the ApplicationBase and PrivateBinPath properties. 您可以藉由建立字串限制所選目錄的複製,該字串只包含您想要陰影複製的目錄,並指派字串至 ShadowCopyDirectories 屬性。You can limit the copying to selected directories by creating a string that contains only those directories you want to shadow copy, and assigning the string to the ShadowCopyDirectories property. 請以分號分隔目錄。Separate the directories with semicolons. 只會對在所選目錄中的組件陰影複製。The only assemblies that are shadow copied are the ones in the selected directories.

    注意

    如果您不指派字串給 ShadowCopyDirectories 屬性,或如果您將這個屬性設定為 null,則會陰影複製 ApplicationBasePrivateBinPath 屬性所指定目錄中的所有組件。If you don’t assign a string to the ShadowCopyDirectories property, or if you set this property to null, all assemblies in the directories specified by the ApplicationBase and PrivateBinPath properties are shadow copied.

    重要

    目錄路徑不可包含分號,因為分號是分隔符號字元。Directory paths must not contain semicolons, because the semicolon is the delimiter character. 分號沒有逸出字元。There is no escape character for semicolons.

啟動效能Startup Performance

當使用陰影複製的應用程式定義域啟動時,複製應用程式目錄中的組件到陰影複製目錄會有延遲;或當組件已經在該位置時,驗證組件也會有延遲。When an application domain that uses shadow copying starts, there is a delay while assemblies in the application directory are copied to the shadow copy directory, or verified if they are already in that location. 在 .NET Framework 4 之前,系統會將所有組件複製到暫存目錄。Before the .NET Framework 4, all assemblies were copied to a temporary directory. 會開啟每個組件來確認組件名稱,且會驗證強式名稱。Each assembly was opened to verify the assembly name, and the strong name was validated. 會檢查每個組件,以查看它是否已更新為比陰影複製目錄中的複本還要新。Each assembly was checked to see whether it had been updated more recently than the copy in the shadow copy directory. 若是如此,則將它複製到陰影複製目錄中。If so, it was copied to the shadow copy directory. 最後會捨棄暫時複本。Finally, the temporary copies were discarded.

從 .NET Framework 4 開始,預設啟動行為會直接比較應用程式目錄中每個組件的檔案日期和時間,以及陰影複製目錄中複本的檔案日期和時間。Beginning with the .NET Framework 4, the default startup behavior is to directly compare the file date and time of each assembly in the application directory with the file date and time of the copy in the shadow copy directory. 如果已更新組件,便會使用和舊版 .NET Framework 相同的程序複製組件;否則會載入陰影複製目錄中的複本。If the assembly has been updated, it is copied by using the same procedure as in earlier versions of the .NET Framework; otherwise, the copy in the shadow copy directory is loaded.

對於組件不常變更,且變更通常發生在組件一小部分的應用程式而言,會產生最大的效能改進。The resulting performance improvement is largest for applications in which assemblies do not change frequently and changes usually occur in a small subset of assemblies. 如果應用程式中大部分的組件經常變更,則新的預設行為可能會導致效能變差。If a majority of assemblies in an application change frequently, the new default behavior might cause a performance regression. 您可以藉由將 <shadowCopyVerifyByTimestamp> 項目 (並設定 enabled="false") 新增至組態檔,還原舊版 .NET Framework 的啟動行為。You can restore the startup behavior of previous versions of the .NET Framework by adding the <shadowCopyVerifyByTimestamp> element to the configuration file, with enabled="false".

已淘汰的方法Obsolete Methods

AppDomain 類別有幾種方法,例如 SetShadowCopyFilesClearShadowCopyPath,可用來控制應用程式定義域上的陰影複製,但這些已經在 .NET Framework 2.0 版中標記為已淘汰。The AppDomain class has several methods, such as SetShadowCopyFiles and ClearShadowCopyPath, that can be used to control shadow copying on an application domain, but these have been marked obsolete in the .NET Framework version 2.0. 建議使用 AppDomainSetup 類別的屬性來設定用於陰影複製的應用程式定義域。The recommended way to configure an application domain for shadow copying is to use the properties of the AppDomainSetup class.

另請參閱See also