the .NET Framework has in place to deal with versioning problems.

Two Kinds of Assemblies, Two Kinds of Deployment

  weakly named assemblies 、 strongly named assemblies

  same:use the same portable executable (PE) file format, PE32(+) header, CLR header, metadata, manifest tables, and Intermediate Language (IL).use the same tools, such as the C# compiler and AL.exe

  different:strongly named assembly is signed with a publisher’s public/private key pair that uniquely identifies the assembly’s publisher.it allows the assembly to be deployed anywhere on the user’s machine or even on the Internet.allows the CLR to enforce certain known-to-besafe policies when an application tries to bind to a strongly named assembly

 

  be deployed in two ways: privately 、 globally

  

Giving an Assembly a Strong Name

  qustion:Two (or more) companies could produce assemblies that have the same file name. Then, if both of these assemblies get copied into the same well-known directory, the last one installed wins, and all of the applications that were using the old assembly no longer function as desired. (This is exactly why DLL hell exists today in Windows, in which shared DLLs are all just copied into the System32 directory.)

  answer:differentiating assemblies simply by using a file name isn’t good enough.The CLR needs to support some mechanism that allows assemblies to be uniquely identified.(strongly named assembly.

  A strongly named assembly consists of four attributes that uniquely identify the assembly: a file name (without an extension), a version number, a culture identity, and a public key.

  

  standard public/private key cryptographic technologies(allows two companies to create assemblies that have the same name, version, and culture without causing any conflict.)

  1.creating a strongly named assembly is to obtain a key(SN.exe)

    SN –k MyCompany.snk

    SN –p MyCompany.snk MyCompany.PublicKey sha256

    SN –tp MyCompany.PublicKey

    

  2.signs the assembly with the private key, and embeds the public key in the manifest.

    csc /keyfile:MyCompany.snk Program.cs

    或者,using Visual Studio, you can create a new public/private key file by displaying the properties for your project, clicking the Signing tab, selecting the Sign The Assembly check box, and then choosing the <New…> option from the Choose A Strong Name Key File combo box.

  

  The AssemblyRef metadata information (obtained by using ILDasm.exe)

    

The Global Assembly Cache

  how to deploy this assembly and how the CLR uses the information to locate and load the assembly.

  question:If an assembly is to be accessed by multiple applications, the assembly must be placed into a wellknown directory, and the CLR must know to look in this directory automatically when a reference to the assembly is detected

  answer:This well-known location is called the global assembly cache (GAC). The exact location of the GAC is an implementation detail that is subject to change with different versions of the .NET Framework(%SystemRoot%\Microsoft.NET\Assembly)

  an algorithm is used to generate the names of these subdirectories.should never manually copy assembly files into the GAC;instead, you should use tools to accomplish this task(GACUtil.exe)

  Installing assemblies into the GAC breaks the goal of simple application installation, backup, restore, moving, and uninstall. So it is recommended that you avoid global deployment and use private deployment whenever possible.

Building an Assembly That References a Strongly Named Assembly

  how to use CSC.exe’s /reference compiler switch to specify the assembly file names you want to reference.

  If the file name is a full path, CSC.exe loads the specified file and uses its metadata information to build the assembly. As mentioned in Chapter 2, if you specify a file name without a path, CSC.exe attempts to find the assembly by looking in the following directories (in order of their presentation here):

  1. Working directory.

  2. The directory that contains the CSC.exe file itself. This directory also contains the CLR DLLs.

  3. Any directories specified using the /lib compiler switch.

  4. Any directories specified using the LIB environment variable.

  The files in the compiler/CLR directory exist so that you can easily build your assembly,whereas the copies in the GAC exist so that they can be loaded at run time.

Strongly Named Assemblies Are Tamper-Resistant

  Signing an assembly with a private key and embedding the signature and public key within an assembly allows the CLR to verify that the assembly has not been modified or corrupted.

  1.the CLR uses the referenced assembly’s properties (name, version, culture, and public key) to locate the assembly in the GAC

  2.the CLR looks in the application’s base directory

  3.in any of the private paths identified in the application’s configuration file;

  4.if the application was installed using MSI, the CLR asks MSI to locate the assembly

  5.If the assembly can’t be found in any of these locations, the bind fails, and a System.IO.FileNotFoundException is thrown;if founded,its containing subdirectory is returned, and the file holding the manifest is loaded(assures the caller that the assembly loaded at run time came from the same publisher that built the assembly the code was compiled against.This assurance is possible because the public key token in the referencing assembly’s AssemblyRef table corresponds to the public key in the referenced assembly’s AssemblyDef table.)

Delayed Signing

  When you’re ready to package your strongly named assembly, you’ll have to use the secure private key to sign it.

  question:while developing and testing your assembly, gaining access to the secure private key can be a hassle.

  answer:the .NET Framework supports delayed signing, sometimes referred to as partial signing

  Delayed signing allows you to build an assembly by using only your company’s public key; the private key isn’t necessary.

  will lose all of the tampering protection afforded to you because the assembly’s files won’t be hashed, and a digital signature won’t be embedded in the file.

  develop your assembly by using the delayed signing technique:

  1. While developing an assembly, obtain a file that contains only your company’s public key, and compile your assembly by using the /keyfile and /delaysign compiler switches.

  csc /keyfile:MyCompany.PublicKey /delaysign MyAssembly.cs

  2. After building the assembly, execute the following line so that the CLR will trust the assembly’s bytes without performing the hash and comparison. This allows you to install the assembly in the GAC (if you desire). Now, you can build other assemblies that reference the assembly, and you can test the assembly. Note that you have to execute the following command line only once per machine; it’s not necessary to perform this step each time you build your assembly.  SN.exe –Vr MyAssembly.dll

  3. When ready to package and deploy the assembly, obtain your company’s private key, and then execute the following line. You can install this new version in the GAC if you desire, but don’t attempt to install it in the GAC until executing step 4.

  SN.exe -Ra MyAssembly.dll MyCompany.PrivateKey
  4. To test in real conditions, turn verification back on by executing the following command line.
  SN.exe –Vu MyAssembly.dll

Privately Deploying Strongly Named Assemblies

  the GAC is usually secured so that only an administrator can install an assembly into it. Also, installing into the GAC breaks the simple copy deployment story.

  you deploy assemblies into the GAC only if the assembly is intended to be shared by many applications.If an assembly isn’t intended to be shared, it should be deployed privately.

How the Runtime Resolves Type References

  

Advanced Administrative Control (Configuration)

  how an administrator can affect the way the CLR searches and binds to assemblies

  how a referenced assembly’s files can be moved to a subdirectory of the application’s base directory

  how the CLR uses the application’s XML configuration file to locate the moved files.

Publisher Policy Control

  question:when a publisher fixes a bug in an assembly,the publisher would like an easy way to package and distribute the new assembly to all of the users.

  answer:What the publisher needs is a way to create policy information that is installed on the user’s computer when the new assembly is installed.

  packaged together with the new Some-ClassLibrary.dll assembly file and deployed to users.

  1.create an XML configuration file

  

  2.assembly by running AL.exe as follows.

  AL.exe /out:Policy.1.0.SomeClassLibrary.dll

      /version:1.0.0.0
      /keyfile:MyCompany.snk
      /linkresource:SomeClassLibrary.config

  the administrator would like to tell the CLR to ignore the publisher policy assembly:edit the application’s configuration file and add the following publisherPolicy element(<publisherPolicy apply="no"/>).

Shared Assembilies and Strongly Named Assemblies的更多相关文章

  1. CLR via C# 3rd - 03 - Shared Assemblies and Strongly Named Assemblies

    1. Weakly Named Assembly vs Strong Named Assembly        Weakly named assemblies and strongly named ...

  2. .NET:CLR via C# Shared Assemblies and Strongly Named Assemblies

    Two Kinds of Assemblies, Two Kinds of Deployment A strongly named assembly consists of four attribut ...

  3. Chapter 3 Shared Assemblies and Strongly Named Assemblies

    As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...

  4. .Net中DLL冲突解决(真假美猴王)

    <西游记>中真假美猴王让人着实难以区分,但是我们熟知了其中的细节也不难把他们剥去表象分别出来.对问题不太关心的可以直接调到文中关于.Net文件版本的介绍 问题 最近在编译AKKA.net ...

  5. NET Framework GAC目录构造

    转:http://www.yl1001.com/userzone.htm?doaction=article&art_id=5851381388387201 我们一般都知道,.NET Frame ...

  6. error——Fusion log——Debugging Assembly Loading Failures

    原文 So...you're seeing a FileNotFoundException, FileLoadException, BadImageFormatException or you sus ...

  7. Position Independent Code (PIC) in shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/下一文: ...

  8. Polly

    Polly Polly is a .NET 3.5 / 4.0 / 4.5 / PCL (Profile 259) library that allows developers to express ...

  9. .NET:CLR via C# Manifest

    An assembly is a collection of one or more files containing type definitions and resource files. One ...

随机推荐

  1. ID3DXMesh接口 创建自己的立方体网格

    D3DXCreateMeshFVF 首先创建一个ID3DXMesh接口. ID3DXMesh接口的说明可以参数龙书. 这里主要是用代码来讲解: #define VERTEX_FVF (D3DFVF_X ...

  2. TVideoGrabber的使用(一)捕捉摄像头

    使用TVideoGrabber捕捉摄像头,相当容易,只需几句代码即可解决问题,首先我们新建一个工程,然后从控件面板上拉取一个 TVideoGrabber控件到窗体中,然后再在窗体上放置四个Button ...

  3. python核心编程学习记录之Python对象

    比较符号如<,>,=比较的是对象的值 如果要比较对象本身要用is,is not repr()的功能与''所做的事情是一样的 Python不支持的类型有char,byte,指针,short, ...

  4. 关于学习keynote

    下午在学习如何用keynote写出高大上的文档,看到公司内的一个妹纸洋洋洒洒的写了好多篇文章,顿时觉得自己的知识面狭窄,文科女和理科女的差别,从我嘴里半天吐不出一个富有诗情画意的词句来,那么还是脚踏实 ...

  5. 一篇文章一张思维导图看懂Android学习最佳路线

    一篇文章一张思维导图看懂Android学习最佳路线 先上一张android开发知识点学习路线图思维导图 Android学习路线从4个阶段来对Android的学习过程做一个全面的分析:Android初级 ...

  6. 将linux下的rm命令改造成移动文件至回收站【转】

    转自:http://blog.csdn.net/a3470194/article/details/16863803 [-] 将linux下的rm命令改造成移动文件至回收站 将AIX下的rm命令改造成移 ...

  7. 图示-Centos7完整安装

    工作过程中,一些未接触过Centos,或未安装过Centos的同事经常会问,如何安装?这个事说简单真简单,只有操作过一次,第二次就能够熟练的自己动手安装:但说难也难,如果没人带,第一次安装的时候确实不 ...

  8. C#中的托管和非托管

    注意!先看左上角声明!我不想误人子弟!但我不怕思考.没有思考就没有错误,互相学习,共同进步! 百度中的“专业人士”回答中出现了这句话(不知道是不是专业人士啊 百度说的)“1.是指托管代码,托管代码(M ...

  9. 在PostgreSQL中使用oracle_fdw访问Oracle

    本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...

  10. C++ Template Operator

    #include <iostream> #include <string> #include <deque> #include <stdexcept> ...