The CLR about strong-named assemblies.

Source: Internet
Author: User

How to create a strongly named assembly (Strong Name Assembly)

Creating a strong-named assembly requires first obtaining a strong naming utility
(Strong Name Utility, i.e.SN.exe,. NET SDKThe resulting key.
Here's a brief introductionSN.exeSome of the usage.To generate a public key/Private Key pair:

A) Sn–k Mycompany.keys
The name tellsSN.exeCreate a name that is namedMycompany.keysThe file.Mycompany.keysThe file will contain a public key and a private key that is stored in a binary format.

bTo view the public key:
First, generate a file that contains only the public key:Sn–p
Mycompany.keys Mycompany.publickey
and then use-tpParameter view:SN–TP Mycompany.publickeys
Public key is

00240000048000009400000006020000002400005253413

10004000001000100bb7214723ffc13901343df4b9c464ebf

7ef4312b0ae4d31db04a99673e8163768cc0a2a7062e731d

Beb83b869f0509bf8009e90db5c8728e840e782d2cf928dae

35c2578ec55f0d11665a30b37f8636c08789976d8ee9fe9a5

C4a0435f0821738e51d6bdd6e6711a5acb620018658cce93

Df37d7e85f9a0104a5845053995ce8

Public Key token is 2DC940D5439468C2

The public key is created/Private key pair, it's easy to create a strong-named assembly. Only need to putSystem.Reflection.AssemblyKeyFileAttributeAttributes are added to the source code:? [Assembly:assemblykeyfile ("Mycompany.keys")]

Description: Public key/The private key's extension to the file can be arbitrary (or not), because the compilation is read in the form of metadata.

4.How assemblies are deployed
There are two ways to deploy an assembly:
APrivate Way
Assemblies that are deployed in the same directory as applications are called private deployment assemblies. Weakly named assemblies can only be privately deployed.

bGlobal mode
The global deployment method deploys the assembly in someClrKnown place, whenClrWhen you search the assembly, it will know where to look. Strong-Named assemblies can be either privately deployed or globally deployed.

5.How to deploy a strongly named assembly (Strong Name Assembly) andGac
A) GACThe concept
If aAssemblyTo be accessed by multiple applications, he must be placed in aClrThe known directory, andClrIn the detection of aAssemblyReference, it must be able to find the assembly automatically in that directory. This known directory is calledGac(Global Assembly Cache), which is the global assembly cache. It is generally located under the following directory:<system DRIVE&GT;:/WINDOWS/ASSEMBLY/GAC。
GacThe role is to provideClrA known set of directories to look for references to theAssembly.

b) GACThe internal structure
Gacis a special structured directory, withWindows ExplorerBrowse you'll think it's just a normal directory with lots of assemblies. In fact, this is not the case, in the command line view, you will find that it actually contains many subdirectories, the name of the subdirectory and the name of the assembly is the same, but they are not the actual assembly, the actual assembly is located in the directory corresponding to the assembly name. such as enteringGcfwksubdirectory, we will find that there are many subdirectories. Each installation in the machineGacOfGCFWK.dllInGcfwkwill have a subdirectory in it.


This is onlyThere is a directory that indicates that there is only one version ofGcfwkAssembly is installed. The actual assembly is saved in each corresponding version directory. The name of the table of contents is underlined in the form of the"(Version) _ (Culture) _ (PublicKeyToken)"。

GcfwkLanguage and culture information forNetture, it is expressed as0.0.0__bf5779af662fc055 "。The meaning of the expression is:"Gcfwk, version=1.0.0.0, culture=neutral,publickeytoken=bf5779af662fc055"If the language and culture information is"Ja", it means"1.0.0.0_ja_bf5779af662fc055"

The meaning of the expression is:"Gcfwk, version=1.0.0.0, Culture=ja, publickeytoken=bf5779af662fc055"

CDeploy a strong-named assembly toGac

GacContains many subdirectories, which are generated using an algorithm, and we'd better not copy the assembly manually toGac, instead, we should use tools to do the work. Because these tools knowGacThe internal structureJ

In development and testing, the most common tools areGACUtil.exe。InGacRegisters the assembly withComRegistration is pretty much, but relatively easy:
1. Add the assembly to theGacIngacutil/i Sample.dll(Parameter/Iis the installation meaning)
2. Move the Assembly outGAC gacutil/u Sample.dll(Parameter/uThe meaning of the removal)
Note: You cannot install a weak-named assembly intoGacIn
If you try to add a weak named assembly to theGac, you receive the error message:”
Failure adding assembly to the Cache:attempt-install an assembly without a strong name "
DPrivate deployment of strong-Named assemblies

Install the assembly toGacThere are several benefits. First of allGacMakes it possible for many programs to share assemblies, which reduces the overall amount of physical memory used; second, it's easy to deploy a new version of the Assembly toGac, and through a publisher policy (almost a redirection method, such as the original reference version as1.0.0.0assembly, by changing its configuration file to let the program refer to the version2.0.0.0Assembly) To use the new version;GacAlso provides the coexistence of different versions of assemblies (Side-by-side) management method. However, gacgac.NET< Span style= "FONT-SIZE:9PT;" > The promise of a simple copy deployment of the framework.
  
    except to gac xml configuration files that point to a public directory so that, at run time, clr will know where to find this strong named assembly. But this could trigger .net strong naming policy:

Generates a public and private key pair, and strict protection of the private key
Build: Sn-k keyfile.snk Public and private key pair
Extract Public Key: Sn-p keyfile.snk public.snk extract public key from KeyFile to public.snk file for late signing of assembly
Skip Validation: SN-VR Assembly to Assembly on the development machine to skip validation processing, the assembly is not signed
Signature: Sn-r assembly KEYFILE.SNK Late signing of assembly, this step is done before the release
Cancel Skip Validation: Sn-vu Assembly to Assembly on the development machine to cancel the skip verification, the assembly is signed
Or Cancel All: SN-VX Cancel all validations

Development phase
Assembly is not a signed, but strongly named strong named, so it needs to be done on the development machine Skip ValidationProcessing

Release phase
Assembly by the private key controller Signatureand developed on the developer's own Cancel Skip Validation

The CLR about strong-named assemblies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.