使用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