使用config管理Assembly。
以下是例子
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="AuxFiles;bin\subdir" />
<dependentAssembly>
<assemblyIdentity name="MyType"
publicKeyToken="32ab4ba45e0a69a1" culture="neutral"/>
<bindingRedirect
oldVersion="1.0.0.0" newVersion="2.0.0.0" />
<codeBase version="2.0.0.0"
href="http://www.MyType.com/MyType.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="TypeLib"
publicKeyToken="1f2e74e897abbcfe" culture="neutral"/>
<bindingRedirect
oldVersion="3.0.0.0-3.5.0.0" newVersion="4.0.0.0" />
<publisherPolicy apply="no" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
對應節點含義為:
probing element
尋找弱命名Assembly在AuxFiles;bin\subdir下。
dependentAssembly, assemblyIdentity, and bindingRedirect elements
當尋找版本為1.0.0.0 public key為32ab4ba45e0a69a1,名為MyType的Assembly的時候,替換為版本2.0的Assembly。
codeBase element
指定版本為2.0的MyType.dll的位置
Public Policy
當你很小範圍的修改了以前的Assembly,如對前面的版本修正了一些Bug,此時你想讓客戶的程式很快接受你的變化。
前面已經描述了我們可以修改程式config或者machine config,但是這有時候太麻煩。
我們這時候就可以用Public Policy來適應這種變化。
首先,建立一個獨立的config檔案,此處命名為MyType.config
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyType"
publicKeyToken="32ab4ba45e0a69a1" culture="neutral"/>
<bindingRedirect
oldVersion="1.0.0.0" newVersion="2.0.0.0" />
<codeBase version="2.0.0.0"
href="http://www.Mytype.com/MyType.dll/" >
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
然後,開啟AL建立Policy
AL.exe /out:Policy.1.0.MyType.dll
/version:1.0.0.0
/keyfile:MyKey.snk
/linkresource:MyType.config
/out 此處命名很重要!指定輸出檔案名為Policy.1.0.JeffTypes.dll。Policy告訴CLR檔案裡包含Policy資訊。1.0表示該Policy對應處理的Assembly版本。MyType表示對應的Assembly名。
/version 標識此Policy版本。
/keyfile Assembly發布對應的Key。
/linksource 對應剛才建立的config檔案名稱。
發布的時候連同新的dll將此Policy也帶上即可,而不需修改config。
值得注意的是使用Policy另外的好處是,當新版本出現問題想放棄時,可以直接通過config設定。
看到一開始的配置有<publisherPolicy apply="no" />,這裡就表示屏蔽掉Policy