若要說明如何設定應用程式使用最新或相容的共用元件,您必須在組件快取中安裝兩個 2.0 版的 Reverser.dll - 您可以使用 Build.bat 檔執行,它使用全域組件快取工具 (GACUTIL.EXE):
gacutil.exe /i Reverser.dll
安裝這些 Reverser 組件後,您可以巡覽 \WindowsDirectory\Assembly 目錄,並且使用組件快取檢視器 Shell 副檔名來檢查組件快取:
.gif)
現在您可以為要指定 2.0.0.0 版Reverser 元件的 VerClient 可執行檔,進行編譯:
csc /reference:Stringer\Stringer.dll;
... Reverser_v2.0.0.0\Reverser.dll VerClient.cs
正如 (3) 私密元件路徑和繫結原則所述,您可以在 Run Time 時,使用應用程式組態檔來控制尋找和繫結組件。尤其 BindingRedirect 標記可以用新版覆蓋原始參考中的版本,帶您重新參考不同版本的強式名稱組件。下列選項說明 2.0.0.0 版到 2.0.0.9 版之間的任何組件參考,在 Run Time 時應該使用 2.0.1.0 版:
<bindingRedirect
oldVersion="2.0.0.0-2.0.0.9" newVersion="2.0.1.0"
/>
這樣系統管理員就能重新設定應用程式,不必重新編譯。
5_Versioned 子目錄中的範例 VerClient.exe.config 檔,示範這個選項。
清單 2 VerClient.exe 的組態檔 (VerClient.exe.config)
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Stringer"/>
<publisherPolicy apply="no"/>
<dependentAssembly>
<assemblyIdentity name="Reverser"
publicKeyToken="0038acc8beadf1e5"
culture=""/>
<publisherPolicy apply="no"/>
<bindingRedirect oldVersion="2.0.0.0"
newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
2.0.1.0 版 Reverser.dll 中的型別方法已經刻意弄成不相容於 2.0.0.0 版中的相同方法,因此 2.0.0.0 版的相容用戶端呼叫後面的修訂內容時會失敗。只要將:
newVersion="2.0.0.0"
變更為:
newVersion="2.0.1.0"
即可示範這個行為,它會產生 MissingMethodException。更常見的是當應用程式被後續安裝的共用元件相同但是版本不同的應用程式中斷時,這個機制允許系統管理員修復應用程式,讓它繼續執行。
最後需要清除應用程式時,您應該從組件快取中移除共用元件:
gacutil /u reverser
這個機制會移除組件快取中的所有元件版本。如果元件還在應用程式的私密路徑中或是還在和元件同名的子目錄中,以後會陸續從這裡載入。