程序集、应用程序配置及App.config和YourSoft.exe.config .
轉自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html
本章概要:
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).aspx
?
3:強名稱的程序集
???? 強名稱是由程序集的標識加上公鑰和數字簽名組成的,其中,程序集的標識包括簡單文本名稱、版本號和區域性信息(如果提供的話)。它使用對應的私鑰從程序集文件中生成。(程序集文件包含程序集清單,其中包含組成程序集的所有文件的名稱和哈希。)
???? 具有強名稱的程序集只能使用其他具有強名稱的程序集的類型。否則將會危及到該具有強名稱的程序集的安全。
?
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。
?
5:使用 DEVPATH 查找程序集
???? 開發人員可能想確保他們正在生成的共享程序集能與多個應用程序一起正常使用。在開發周期內開發人員不用頻繁地將程序集放在全局程序集緩存中,他們可以創建 DEVPATH 環境變量,讓該變量指向程序集的生成輸出目錄。
???? 例如,假設您正在生成名為 MySharedAssembly 的共享程序集,且輸出目錄是 C:/MySharedAssembly/Debug。可以將 C:/MySharedAssembly/Debug 置于 DEVPATH 變量中。然后必須在計算機配置文件中指定 <developmentMode> 元素。該元素告訴公共語言運行庫使用 DEVPATH 來查找程序集。
???? 共享程序集必須能夠由運行庫發現。 若要指定用于解析程序集引用的私有目錄,請在配置文件中使用 <codeBase> 元素 或 <probing> 元素,如 指定程序集的位置 中所述。 還可以將程序集放在應用程序目錄的子目錄中。有關更多信息,請參見運行庫如何定位程序集。
???? 下面的示例說明如何使運行庫在由 DEVPATH 環境變量所指定的目錄中搜索程序集。
<configuration><runtime><developmentMode developerInstallation="true"/></runtime> </configuration>?
6:指定要使用的運行庫版本
<?xml version ="1.0"?> <configuration><startup><supportedRuntime version="v1.1.4322" /> </startup> </configuration>?
7:App.config和YourSoft.exe.config
???? 為了更加快速的使用配置信息而不自己寫代碼實現讀寫,我們在創建應用程序的時候應該使用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>?????
<assemblyIdentity name="Assembly1" publicKeyToken="32ab4ba45e0a69a1" language="en-US"?
version="*.*.*.*" />???????
<bindingRedirect newVersion="*.*.*.*"/>????
</dependentAssembly>
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.???????
<configuration>?????
<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>??
</configuration>
C. Add the following XML element to the machine configuration file.??????????
<configuration>?????
<startup>???????
<requiredRuntime version="1.1.4322" />???????
<startup>???
</configuration>
D.? Add the following XML element to the machine configuration file.??????????
<configuration>?????
<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>??
</configuration>
Answer: A
?
總結
以上是生活随笔為你收集整理的程序集、应用程序配置及App.config和YourSoft.exe.config .的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到吃生花生是什么意思
- 下一篇: UVa 11136 - Hoax or