C#筆記29:程式集及應用程式配置及App.config和YourSoft.exe.config
本章概要:
1:什麼是程式集
2:程式集辨別屬性
3:強名稱的程式集
3.1:強名稱工作原理
4:配置檔案
5:使用 DEVPATH 查找程式集
6:指定要使用的運作庫版本
7:App.config和YourSoft.exe.config
1:什麼是程式集
程式集是 .NET Framework 應用程式的構造塊;程式集構成了部署、版本控制、重複使用、激活範圍控制和安全權限的基本單元。程式集是為協同工作而生成的類型和資源的集合,這些類型和資源構成了一個邏輯功能單元。程式集向公共語言運作庫提供了解類型實作所需要的資訊。
程式集屬性是提供程式集相關資訊的值。屬性分為以下幾組資訊:
- 程式集辨別屬性。
- 資訊性屬性。
- 程式集清單屬性。
- 強名稱屬性。
2:程式集辨別屬性
三種屬性與強名稱(如果适用)一起決定程式集的辨別:名稱、版本和區域性。這些屬性構成程式集的完整名稱,并且在代碼中引用程式集時需要這些屬性。您可以使用屬性來設定程式集的版本和區域性。編譯器或
程式集連結器 (Al.exe)根據包含程式集清單的檔案在建立程式集時設定名稱值。
有關程式集屬性的更多資訊,參看
http://msdn.microsoft.com/zh-cn/library/4w8c1y2s(VS.80).aspx3:強名稱的程式集
強名稱是由程式集的辨別加上公鑰和數字簽名組成的,其中,程式集的辨別包括簡單文本名稱、版本号和區域性資訊(如果提供的話)。它使用對應的私鑰從程式集檔案中生成。(程式集檔案包含程式集清單,其中包含組成程式集的所有檔案的名稱和哈希。)
具有強名稱的程式集隻能使用其他具有強名稱的程式集的類型。否則将會危及到該具有強名稱的程式集的安全。
3.1:強名稱工作原理
簽名機制
1. 用SN.exe 生成一個key檔案, 這個key檔案包括一個public key 和一個private key.
2. 用這個key檔案簽名assembly時, 編譯器将用private key簽名程式集, 并将public key嵌入manifest中
3. 編譯器哈希manifest中所有的assembly内容, 并将此哈希值各自assembly的FileDef Talbe中.
4. 當如上3步處理後, 編譯器将哈希PE檔案的整個内容(除authenticode signature, 強名稱資料, PE頭), 然後将此哈希值用private key簽名. 得到RSA數字簽名.
5. 将此數字簽名嵌入到PE檔案的CLR頭
防修改機制
1. 當簽名後的assembly安裝到GAC, 系統會哈希PE檔案(同簽名), 得到哈希值
2. 系統讀取PE檔案中CLR頭中的RSA簽名, 并用public key(包含在manifest中)解密
3. 比較第1步得到的哈希值和第2步得到解密值是否一緻, 進而判斷其是否被修改.
4:配置檔案
配置檔案是可以按需要更改的 XML 檔案。開發人員可以使用配置檔案來更改設定,而不必重編譯應用程式。管理者可以使用配置檔案來設定政策,以便影響應用程式在計算機上運作的方式。
配置檔案更多内容檢視
http://msdn.microsoft.com/zh-cn/library/1xtk877y(VS.90).aspx。
開發人員可能想確定他們正在生成的共享程式集能與多個應用程式一起正常使用。在開發周期内開發人員不用頻繁地将程式集放在全局程式集緩存中,他們可以建立 DEVPATH 環境變量,讓該變量指向程式集的生成輸出目錄。
例如,假設您正在生成名為 MySharedAssembly 的共享程式集,且輸出目錄是 C:\MySharedAssembly\Debug。可以将 C:\MySharedAssembly\Debug 置于 DEVPATH 變量中。然後必須在計算機配置檔案中指定
<developmentMode>元素。該元素告訴公共語言運作庫使用 DEVPATH 來查找程式集。
共享程式集必須能夠由運作庫發現。 若要指定用于解析程式集引用的私有目錄,請在配置檔案中使用
<codeBase> 元素或
<probing> 元素,如
指定程式集的位置中所述。 還可以将程式集放在應用程式目錄的子目錄中。有關更多資訊,請參見
運作庫如何定位程式集。
下面的示例說明如何使運作庫在由 DEVPATH 環境變量所指定的目錄中搜尋程式集。
<configuration>
<runtime>
<developmentMode developerInstallation="true"/>
</runtime>
</configuration>
<?xml version ="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v1.1.4322" />
</startup>
</configuration>
為了更加快速的使用配置資訊而不自己寫代碼實作讀寫,我們在建立應用程式的時候應該使用App.config。建立完畢後,我們發現App.config的屬性是:

以上是建立App.config後的預設設定,不要修改這些屬性,編譯你的解決方案,會在輸出目錄中發現生成了一個YourSoft.exe.config(假設你的應用程式名為YourSoft.exe)。下面有幾點需要說明:
1:YourSoft.exe.config其實對應的就是你解決方案中的App.config。注意,千萬不要以為在輸出目錄中它也會以App.config存在。
2:如果“複制到輸出目錄”屬性你設定的是“複制”或者“較新則複制”,則App.config會被複制到輸出目錄。千萬不要以為在輸出目錄中的App.config對應用程式會有任何意義。運作時預設還是會去讀YourSoft.exe.config。
3:輸出目錄中YourSoft.exe.config的值不會自動保持和你解決方案中的App.config内容一緻。你需要手動去設定YourSoft.exe.config中的值。
練習:
1.You are creating a strong-named assembly named Asse mbly1 that will be used in multiple applications.
Assembly1 will be rebuilt frequently during the development cycle. You need to ensure that each time the
assembly is rebuilt it works correctly with each application that uses it. You need to configure the computer on
which you develop Assembly1 such that each application uses the latest bu ild of Assembly1. Which two actions
should you perform? (Each correct answer presents part of the solution. Choose two.)
A. Create a DEVPATH environment variable that points to the build output directory for the strong-named
assembly.
B. Add the following XML element to the machine configuration filE.
<developmentMode developerInstallation="true"/>
C. Add the following XML element to the machine configuration filE.
<dependentAssembly>
<assemblyIdentity name="Assembly1" publicKeyToken="32ab4ba45e0a69a1" language="en-US"
version="*.*.*.*" />
<publisherPolicy apply="no" />
</dependentAssembly>
D. Add the following XML element to the configuration file of each application that uses the strong-named
assembly:
<supportedRuntime version="*.*.*.*" />
E. Add the following XML element to the configuration file of each application that uses the strong-named
assembly:
<dependentAssembly>
version="*.*.*.*" />
<bindingRedirect newVersion="*.*.*.*"/>
Answer: A, B
2.Your company uses an application named Application1 that was compiled by using the .NET Framework
version 1.0. The application currently runs on a shared computer on which the .NET Framework versions 1.0 and
1.1 are installed. You need to move the application to a new computer on which the .NET Framework versions
1.1 and 2.0 are installed. The application is compatible with the .NET Framework 1.1, but it is incompatible with
the .NET Framework 2.0. You need to ensure that the application will use the .NET Framework version 1.1 on the
new computer. What should you do?
A. Add the following XML element to the application configuration file.
<configuration>
<startup>
<supportedRuntime version="1.1.4322" />
<startup>
</configuration>
B. Add the following XML element to the application configuration file.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Application1"
publicKeyToken="32ab4ba45e0a69a1" culture="neutral" />
<bindingRedirect oldVersion="1.0.3075.0" newVersion="1.1.4322.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
C. Add the following XML element to the machine configuration file.
<requiredRuntime version="1.1.4322" />
D. Add the following XML element to the machine configuration file.
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Answer: A
本文基于
Creative Commons Attribution 2.5 China Mainland License釋出,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名
http://www.cnblogs.com/luminji(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。