How to Create a strongly-named assembly

Source: Internet
Author: User
How to create a strong name Program Set ( Strong name assembly )

To create a strongly-named assembly, you must first obtain a utility that uses a strongly-named
( Strong name Utility , That is Sn.exe , . Net SDK Built-in.
The following is a brief introduction. Sn.exe .   Generate a Public Key / Private key pair:

A) Sn-K mycompany. Keys
This name tells Sn.exe Create Mycompany. Keys . Mycompany. Keys The file contains the public key and private key that are stored in binary format.

B) View the public key:
First, generate a file that only contains the public key: Sn-P
Mycompany. Keys mycompany. publickey
Then use -TP View parameters: Sn-TP mycompany. publickeys
Public Key is

00240000048000009400000006020000002400005253413

10004000001000100bb7214723ffc13901343df4b9c464ebf

7ef4312b0ae4d31db04a99673e8163768cc0a2a7062e731d

Beb83b869f0509bf8009e90db5c8728e840e782d2cf928dae

35c2578ec55f0dda-65a30b37f8636c08789976d8ee9fe9a5

C4a0435f0821738e51d6bdd6e6711a5acb620018658cce93

Df37d7e85f9a0da-a5821353995ce8

Public Key token is 2dc940d5439468c2

Public Key created / The private key pair makes it easy to create a strongly-named assembly. You only need System. reflection. assemblykeyfileattribute Add featuresSource codeYou can: ? [Assembly: assemblykeyfile ("mycompany. Keys")]

Description: Public Key / The extension of the private key pair file can be arbitrary (or not), because it is read in metadata format during compilation.

4. Assembly deployment Method
An assembly can be deployed in two ways:
A) Private Mode
An assembly deployed in the same directory as an application is called a private deployment assembly. Weak naming Assembly can only be deployed in private mode.

B) Global Mode
The global deployment method deploys the Assembly in some CLR Known, when CLR When searching for a dataset, it will know where to find it. Strongly-named assembly can be either private or global.

  5. How to deploy a strongly-named assembly ( Strong name assembly ) And GAC
A) GAC Concept
If Assembly To be accessed by multiple applications CLR And CLR This Assembly It must be able to automatically search for the Assembly in this directory. This known directory is called GAC ( Global Assembly Cache Is the global assembly cache. It is generally located in the following directory: <System drive>: \ windows \ Assembly \ GAC .
GAC Is to provide CLR A known and definite directory to search for referenced   Assembly.

B) GAC Internal Structure
GAC Is a special structured directory Windows Explorer You will think it is just a common directory that contains many sets. In fact, this is not the case. You can view it in the command line and find that it actually contains many sub-directories. The sub-directory names and Assembly names are the same, but they are not the actual assembly, the actual Assembly is located in the directory corresponding to the Assembly name. For example Gcfwk Sub-directories, we will find that there are many sub-directories. Install GAC Of Gcfwk. dll In Gcfwk There will be a sub-directory.

Here only   A directory indicates that only one version Gcfwk The Assembly is installed. The actual program set is saved in the directory of each corresponding version. The directory names are separated "(Version) _ (Culture) _ (publickeytoken )" .

Gcfwk The Language and Culture Information is Netture , Indicates 0.0.0 _ Bf5779af662fc055" .   It indicates: "Gcfwk, version = 1.0.0.0, culture = neutral, publickeytoken = bf5779af662fc055" If the language and culture information is "Ja" , Indicates "1.0.0.0 _ ja_bf5779af662fc055"

It indicates: "Gcfwk, version = 1.0.0.0, culture = JA, publickeytoken = bf5779af662fc055"

C) Deploy strongly-named assembly GAC

GAC Contains many subdirectories.AlgorithmWe 'd better not manually copy the Assembly GAC On the contrary, we should use tools to do this. Because these tools know GAC Internal Structure J

The most common tool in development and testing is Gacutil.exe .   In GAC And Com Registration is similar, but it is relatively easier:
1 . Add the Assembly GAC Medium: Gacutil/I sample. dll (Parameter /I Is the meaning of installation)
2 . Remove assembly GAC gacutil/u sample. dll (Parameter /U )
Note: you cannot install a weak named assembly GAC .
If you try to add the weak naming assembly GAC Will receive the error message: "
Failure adding assembly to the cache: Attempt to install an assembly without a strong name"
D) Private deployment of Strongly-named assembly

Install the Assembly GAC There are several benefits. First, GAC So that many programs can share the assembly, which reduces the physical memory used as a whole. Secondly, we can easily deploy a new version of the assembly GAC And adopt a publisher policy (almost a redirection method, for example 1.0.0.0 The program of the Assembly changes its configuration file to allow the program to reference the version 2.0.0.0 To use the new version. Finally, GAC It also provides ( Side-by-side ) Management method. However, GAC The security policy can only be changed by the Administrator. GAC The Assembly is also damaged. . Net The promise of simple copy deployment of the framework.

Except GAC You can also deploy a strongly-named assembly in a private deployment mode in a directory that is only known to a small number of programs. Configure XML Configuration files to point them to a public directory, so that at runtime, CLR You will know where to find this strongly-named assembly. However, this may cause "DLL hell" Because no program can control when the Assembly is uninstalled. In . Net Is not encouraged.

Strong naming policy:

Generate a public key and private key pair, andStrictly protect private keys
Generate: Sn-K Keyfile. SNK public key and private key pair
Extract Public Key: Sn-P Keyfile. SNK public. SNK extract the public key from Keyfile and save it to the public. SNK file to sign the Assembly later.
Skip Verification: Sn-VR Assembly performs skip verification on the Assembly on the development machine. The Assembly is not signed.
Signature: Sn-r Assembly Keyfile. SNK late signature of assembly, this step is done before release
Cancel skip verification: Sn-vu Assembly cancels skip verification on the Assembly on the development machine. The Assembly is signed at this time.
OrCancel all: Sn-VX cancel all verification

Development Phase
Assembly is not signed, but it is strongly named strong named. Therefore, it needs to be done on the development machine.Skip VerificationProcessing

Release Phase
The private key controller performs assemblySignatureAnd developers themselves on the Development MachineCancel skip verification

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.