About strongly-named assembly

Source: Internet
Author: User
How to create a strong-naming assembly (strong name Assembly)

To create a strongly-named assembly, you must first obtain a utility that uses a strongly-named
(Strong name utility, which is included in sn.exe and. Net SDK.
The following describes some usage of sn.exe. To generate a public/private key pair:

A) Sn-K mycompany. Keys
Create a file named mycompany. Keys. The mycompany. Keys file contains the public and private keys stored in binary format.

B) view the public key:
First, generate a file containing only the public key: Sn-P
Mycompany. Keys mycompany. publickey
Then use the-TP parameter to view: Sn-TP mycompany. publickeys
Public Key is

00240000048000009400000006020000002400005253413

10004000001000100bb7214723ffc13901343df4b9c464ebf

7ef4312b0ae4d31db04a99673e8163768cc0a2a7062e731d

Beb83b869f0509bf8009e90db5c8728e840e782d2cf928dae

35c2578ec55f0dda-65a30b37f8636c08789976d8ee9fe9a5

C4a0435f0821738e51d6bdd6e6711a5acb620018658cce93

Df37d7e85f9a0da-a5821353995ce8

Public Key token is 2dc940d5439468c2

After the public key/private key pair is created, it is easy to create a strongly-named assembly. You only need to add the system. reflection. assemblykeyfileattribute feature to the source code :? [Assembly: assemblykeyfile ("mycompany. Keys")]

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

4. Deployment method of the Assembly
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
In the global deployment mode, the Assembly is deployed in some places that the CLR knows. When the CLR searches for an assembly, it will know where to find it. Strongly-named assembly can be either private or global.

5. How to deploy strong-name assembly and GAC
A) Concept of GAC
If an assembly is to be accessed by multiple applications, it must be placed in a directory that is known to the CLR, and when the CLR detects a reference to the Assembly, it must be able to automatically search for the Assembly in this directory. This known directory is called GAC (Global Assembly Cache), which is the global assembly cache. It is generally located in the following directory: <system drive >:\ WINDOWS \ Assembly \ GAC.
The role of GAC is to provide the CLR with a known and definite directory to find the referenced assembly.

B) Internal Structure of GAC
GAC is a special structured directory. If you use Windows Explorer to browse it, 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, if you enter the gcfwk subdirectory, you will find many subdirectories. Each gcfwk. dll installed on the machine to GAC has a subdirectory in gcfwk.

Only one directory indicates that only one version of gcfwk assembly is installed. The actual program set is saved in the directory of each corresponding version. The name of the directory is separated into (Version) _ (Culture) _ (publickeytoken )".

The gcfwk Language and Culture Information is netture, which 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", it indicates "1.0.0.0 _ ja_bf5779af662fc055"

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

C) deploy a strongly-named assembly to GAC

GAC contains many subdirectories, which are generated using an algorithm. We 'd better not manually copy the Assembly to GAC. Instead, we should use tools to do this. Because these tools know the internal structure of GAC J

The most common tool in development and testing is gacutil.exe. Registering an assembly in GAC is similar to registering an assembly with COM, but it is relatively easier:
1. Add the Assembly to GAC: gacutil/I sample. dll (the parameter/I indicates installation)
2. Remove the Assembly from GAC gacutil/u sample. dll (remove the parameter/U)
Note: a weak naming assembly cannot be installed in GAC.
If you try to add the weak naming assembly to GAC, you 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

Installing an assembly to GAC has several advantages. First, GAC enables many programs to share the assembly, which reduces the physical memory used as a whole. Second, we can easily deploy a new version of the assembly to GAC, and through a publisher policy (almost a redirection method, such as changing the configuration file of a program with the original referenced version 1.0.0.0, instead, let the program reference the Assembly whose version is 2.0.0.0 to use the new version. Finally, GAC also provides the side-by-side management mode for the assembly of different versions. However, GAC's security policy usually only allows the Administrator to change. At the same time, installing an assembly in GAC also breaks the promise of a simple copy deployment of the. NET Framework.

In addition to deploying a strongly-named assembly to GAC or in a private deployment mode, we can also deploy the strongly-named assembly in a directory that is only known to a small number of programs. Configure the xml configuration files of each application and point them to a public directory. In this way, the CLR will know where to find the strongly-named assembly at runtime. But this may cause the "DLL hell" problem, because no program can control when the Assembly will be uninstalled. This is not encouraged in. net.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

From: http://blog.csdn.net/jxufewbt/archive/2007/04/25/1584746.aspx

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.