.NET(C#):程式集配置的元素和元素

來源:互聯網
上載者:User

這兩個元素都在<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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.