zoukankan      html  css  js  c++  java
  • C#笔记29:程序集、应用程序配置及App.config和YourSoft.exe.config

    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).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的属性是:

    image_thumb[1]

         以上是创建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

    Creative Commons License本文基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
  • 相关阅读:
    Swool的安装与使用
    Laravel策略(Policy)示例
    Laravel 查询&数据库&模型
    Laravel模板事项
    Laravel验证问题记录
    每日一练:排序题:一大一小,依次类推题
    java 保留小数点后N位数(若干位),几种实现的方式总结
    java 计算器SWT/RAP(版本3)键盘鼠标兼容
    java SWT/Rap 计算器版本2(键盘鼠标兼容)
    键盘按钮keyCode大全
  • 原文地址:https://www.cnblogs.com/luminji/p/1857339.html
Copyright © 2011-2022 走看看