程序集、应用程序配置及App.config和YourSoft.exe.config .
轉(zhuǎn)自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html
本章概要:
1:什么是程序集
2:程序集標(biāo)識屬性
3:強名稱的程序集
3.1:強名稱工作原理
4:配置文件
5:使用 DEVPATH 查找程序集
6:指定要使用的運行庫版本
7:App.config和YourSoft.exe.config
?
1:什么是程序集
???? 程序集是 .NET Framework 應(yīng)用程序的構(gòu)造塊;程序集構(gòu)成了部署、版本控制、重復(fù)使用、激活范圍控制和安全權(quán)限的基本單元。程序集是為協(xié)同工作而生成的類型和資源的集合,這些類型和資源構(gòu)成了一個邏輯功能單元。程序集向公共語言運行庫提供了解類型實現(xiàn)所需要的信息。
???? 程序集屬性是提供程序集相關(guān)信息的值。屬性分為以下幾組信息:
-
程序集標(biāo)識屬性。
-
信息性屬性。
-
程序集清單屬性。
-
強名稱屬性。
2:程序集標(biāo)識屬性
???? 三種屬性與強名稱(如果適用)一起決定程序集的標(biāo)識:名稱、版本和區(qū)域性。這些屬性構(gòu)成程序集的完整名稱,并且在代碼中引用程序集時需要這些屬性。您可以使用屬性來設(shè)置程序集的版本和區(qū)域性。編譯器或程序集鏈接器 (Al.exe) 根據(jù)包含程序集清單的文件在創(chuàng)建程序集時設(shè)置名稱值。??
???? 有關(guān)程序集屬性的更多信息,參看http://msdn.microsoft.com/zh-cn/library/4w8c1y2s(VS.80).aspx
?
3:強名稱的程序集
???? 強名稱是由程序集的標(biāo)識加上公鑰和數(shù)字簽名組成的,其中,程序集的標(biāo)識包括簡單文本名稱、版本號和區(qū)域性信息(如果提供的話)。它使用對應(yīng)的私鑰從程序集文件中生成。(程序集文件包含程序集清單,其中包含組成程序集的所有文件的名稱和哈希。)
???? 具有強名稱的程序集只能使用其他具有強名稱的程序集的類型。否則將會危及到該具有強名稱的程序集的安全。
?
3.1:強名稱工作原理
簽名機制
1. 用SN.exe 生成一個key文件, 這個key文件包括一個public key 和一個private key.
2. 用這個key文件簽名assembly時, 編譯器將用private key簽名程序集, 并將public key嵌入manifest中
3. 編譯器哈希manifest中所有的assembly內(nèi)容, 并將此哈希值各自assembly的FileDef Talbe中.
4. 當(dāng)如上3步處理后, 編譯器將哈希PE文件的整個內(nèi)容(除authenticode signature, 強名稱數(shù)據(jù), PE頭), 然后將此哈希值用private key簽名. 得到RSA數(shù)字簽名.
5. 將此數(shù)字簽名嵌入到PE文件的CLR頭
防修改機制
1. 當(dāng)簽名后的assembly安裝到GAC, 系統(tǒng)會哈希PE文件(同簽名), 得到哈希值
2. 系統(tǒng)讀取PE文件中CLR頭中的RSA簽名, 并用public key(包含在manifest中)解密
3. 比較第1步得到的哈希值和第2步得到解密值是否一致, 從而判斷其是否被修改.
?
4:配置文件
???? 配置文件是可以按需要更改的 XML 文件。開發(fā)人員可以使用配置文件來更改設(shè)置,而不必重編譯應(yīng)用程序。管理員可以使用配置文件來設(shè)置策略,以便影響應(yīng)用程序在計算機上運行的方式。
???? 配置文件更多內(nèi)容查看http://msdn.microsoft.com/zh-cn/library/1xtk877y(VS.90).aspx。
?
5:使用 DEVPATH 查找程序集
???? 開發(fā)人員可能想確保他們正在生成的共享程序集能與多個應(yīng)用程序一起正常使用。在開發(fā)周期內(nèi)開發(fā)人員不用頻繁地將程序集放在全局程序集緩存中,他們可以創(chuàng)建 DEVPATH 環(huán)境變量,讓該變量指向程序集的生成輸出目錄。
???? 例如,假設(shè)您正在生成名為 MySharedAssembly 的共享程序集,且輸出目錄是 C:/MySharedAssembly/Debug。可以將 C:/MySharedAssembly/Debug 置于 DEVPATH 變量中。然后必須在計算機配置文件中指定 <developmentMode> 元素。該元素告訴公共語言運行庫使用 DEVPATH 來查找程序集。
???? 共享程序集必須能夠由運行庫發(fā)現(xiàn)。 若要指定用于解析程序集引用的私有目錄,請在配置文件中使用 <codeBase> 元素 或 <probing> 元素,如 指定程序集的位置 中所述。 還可以將程序集放在應(yīng)用程序目錄的子目錄中。有關(guān)更多信息,請參見運行庫如何定位程序集。
???? 下面的示例說明如何使運行庫在由 DEVPATH 環(huán)境變量所指定的目錄中搜索程序集。
<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
???? 為了更加快速的使用配置信息而不自己寫代碼實現(xiàn)讀寫,我們在創(chuàng)建應(yīng)用程序的時候應(yīng)該使用App.config。創(chuàng)建完畢后,我們發(fā)現(xiàn)App.config的屬性是:
???? 以上是創(chuàng)建App.config后的默認設(shè)置,不要修改這些屬性,編譯你的解決方案,會在輸出目錄中發(fā)現(xiàn)生成了一個YourSoft.exe.config(假設(shè)你的應(yīng)用程序名為YourSoft.exe)。下面有幾點需要說明:
???? 1:YourSoft.exe.config其實對應(yīng)的就是你解決方案中的App.config。注意,千萬不要以為在輸出目錄中它也會以App.config存在。
???? 2:如果“復(fù)制到輸出目錄”屬性你設(shè)置的是“復(fù)制”或者“較新則復(fù)制”,則App.config會被復(fù)制到輸出目錄。千萬不要以為在輸出目錄中的App.config對應(yīng)用程序會有任何意義。運行時默認還是會去讀YourSoft.exe.config。
???? 3:輸出目錄中YourSoft.exe.config的值不會自動保持和你解決方案中的App.config內(nèi)容一致。你需要手動去設(shè)置YourSoft.exe.config中的值。
練習(xí):
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
?
總結(jié)
以上是生活随笔為你收集整理的程序集、应用程序配置及App.config和YourSoft.exe.config .的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到吃生花生是什么意思
- 下一篇: 晚上做梦梦到自己怀孕了怎么回事