這兩個元素都在<configuration> – <runtime> – <assemblyBinding>元素下。
<probing>元素指定CLR探索程式集的子目錄,這個目錄是相對應用程式定義域的基目錄。(可以參考這篇文章:.NET(C#): 小議CLR載入程式集時的目錄探索),這個目錄可以從AppDomain.BaseDirectory或者AppDomainSetup.ApplicationBase擷取:
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
(輸出會是一樣的)
那麼<probing>只會為程式集探索提供更多的目錄,並不會告訴CLR去找什麼程式集。
而另一個元素<codeBase>,正是告訴CLR在哪裡去找程式集,但是和<probing>也有相似之處。
首先<codeBase>元素在<assemblyBinding> - <dependentAssembly>元素下。既然在<dependentAssembly>下,那麼必須提供引用程式集的資訊,即<assemblyIdentity>元素,此元素告訴CLR你在討論哪個引用的程式集,而<codeBase>就是告訴CLR剛才那個引用的程式集具體在哪個位置,CLR你快去找。這裡有一點需要注意。如果<assemblyIdentity>對應的是一個強式名稱程式集,那麼<codeBase>可以指向任何URL(本地檔案或者網路地址都可以),但如果<assemblyIdentity>對應的不是一個強命名的程式集。<codeBase>只可以指嚮應用程式基目錄內的檔案。此時<codeBase>會很受限制並且很像<probing>元素,不過我覺得如果這樣的話,還是用<probing>比較好。
最後舉個例子,比如用<probing>設定一個子目錄dlls,然後把程式引用的程式集放進去(比如名稱是ClassLibrary1),應用程式設定檔如下:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="dlls"/>
</assemblyBinding>
</runtime>
</configuration>
此時用<assemblyIdentity>指定這個ClassLibrary1程式集,然後用<codeBase>定位其位置代替<probing>元素也是可以的。
設定檔如下:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<!-- 不是強命名程式集,不用設定version,publicKeyToken…… -->
<assemblyIdentity name="ClassLibrary1"/>
<!-- 不是強命名程式集,不用設定version, 設定href指定檔案位置 -->
<codeBase href="file:///E:/Visual Studio 2010/Projects/Mgen/bin/Debug/dlls/ClassLibrary1.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>