Understanding and functions of GAC

Source: Internet
Author: User
Tags reflector

I. Role of GAC

Global Assembly Cache is used to store public assemblies that are used by many programs, such as system. Data and system. Windows. forms. In this way, many programs can obtain the Assembly from the GAC without copying all the assembly to the execution directory of the application. For example, if there is no GAC, it is inevitable that the directory of each winform program should be from C: \ WINDOWS \ Microsoft. net \ framework \ VX copy a copy of system. windows. forms. DLL, which is obviously easier to use from the GAC, but also conducive to the Assembly upgrade and version control.

Ii. Strongly-named assembly

Because different companies may develop an assembly with the same name, if these assembly is copied to the same directory, the last installed assembly will replace the previous Assembly. This is why the famous windows "DLL hell" appeared.

Obviously, it is not enough to distinguish an assembly by file name. CLR must support a mechanism to uniquely identify an assembly. This is the so-called strongly-named assembly.

A strongly-named Assembly contains four unique flag Assembly features: File Name (without extension), version number, language and cultural information (if any), and public key.

This information is stored in the Assembly List (manifest. The list contains the Assembly metadata and is embedded in a file of the Assembly.

The following string identifies four different assembly files:

"Mytype, version = 1.0.1.0,

Culture = neutral, publickeytoken = bf5779af662fc055"

"Mytype, version = 1.0.1.0,

Culture = en-US, publickeytoken = bf5779af662fc055"

"Mytype, version = 1.0.2.0,

Culture = neturl, publickeytoken = bf5779af662fc055"

"Mytype, version = 1.0.2.0,

Culture = neutral, publickeytoken = dbe4120289f9fd8a"

If a company wants to uniquely identify its assembly, it must first obtain a public/private key pair and then associate the common key with the Assembly. There are no two companies that have the same public/private key pair. This distinction allows us to create an assembly with the same name, version, and language and cultural information without causing any conflict.

The weak naming Assembly corresponds to the strongly naming assembly. (In fact, it is a common assembly that is not strongly named ). The two types of Assembly have the same structure. All use the same PE file format, PE Header, CLR header, metadata, and list (manifest ). The real difference between the two is that a strongly-named assembly has a publisher's public/private key pair signature, and the public/private key pair uniquely identifies the Assembly publisher. Using public/private key pairs, we can identify the uniqueness of the assembly, implement security policies and version control policies, this unique capability to identify an Assembly enables applications to implement certain "known security" policies (such as trusting a company's assembly) when attempting to bind a strongly-named assembly ).

3. How to Create a strongly-named assembly and view the publickeytoken of the strongly-named assembly

How to Create a strongly-named assembly

==============================

1. Right-click the class library project in Visual Studio and select Properties.

2. Select the signing tab on the left.

3. Check the sign the assembly check box. In the drop-down list, select <new...>.

4. In the pop-up dialog box, name the SNK file. Press OK.

5. Strong name of the Assembly is completed.

How to view the Public Key token of a strongly-naming assembly

======================================

Sometimes you need to reference a self-written strong-name assembly in the web. config file or elsewhere. You need to write a fully qualified name like the following:

Mynamespace. myassembly, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089

The first three parts are relatively easy to obtain, because they are written by yourself. Of course, you know the Assembly name, version, and culture information. the troublesome part is how to obtain the Public Key token of the Self-Signed assembly. A common method is to use reflector to open your own assembly and obtain the token (in fact, reflector will give you the complete information as in the example above ). however, sometimes it seems a little difficult to kill chicken with a knife. if you have already enabled Visual Studio, it is better to just click a menu item in the vs menu to get the answer? The following are the steps.

1. in Visual Studio, open the Tools menu and click the menu item external tools.

2. In the pop-up external tools dialog box, click Add.

3. configure as needed. sn.exe is in different folders in different versions of. the simplest way to find it is to input "where sn.exe" in vs command prompt ". write "-T $ (targetpath)" in the parameter box )". select "use output window ". in this case, the result will be in the output window of. then click OK,

4. result.

5. the result is displayed in the output window. this works normally when you have multiple projects in your solution. you only need to click the project in Solution Explorer, and then click our menu item.

4. How to register your DLL to GAC

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

Example:

C: \ Program Files \ Microsoft Visual Studio 8 \ Vc> gacutil-I f: \ myweb \ balloonshop \ Cl
Asslibrary1 \ bin \ debug \ classlibrary1.dll

In C: \ WINDOWS \ assembly, you will see classlibrary1. The registration is successful.

5. view the content of the GAC file and copy the DLL.

In projects, we often introduce third-party DLL files. Generally, we can copy the required DLL files to a place on the hard disk and add references to the project. This operation is very simple! But sometimes we will encounter such a situation, that is, the DLL to be referenced is in the GAC of the target machine, then we cannot manually copy it out.

In fact, Windows GAC has a corresponding directory, which is generally c: \ windows \ Assembly \. This directory has some special features, it stores the installed and registered class library DLL on the local machine, and does not allow users to directly perform operations on the elements (such as copying, cutting, pasting, and modifying the name ), however, you can directly drag the DLL file in another location to this directory for DLL installation, but we cannot directly copy the installed DLL. Here I will introduce a method to complete this operation.

First, switch to the command line mode of windows, that is, start-run-cmd-press enter, and then go to the directory where GAC is located. Use the Dir command to view the content, as shown in.

It seems that you can understand the directory structure in GAC. Basically, we can distinguish the Dir type based on the processor architecture column in the GAC directory, for example, the system we are looking. web. extensions belongs to msil. In cmd mode, it should correspond to gac_msil, switch to this directory, and Dir.

We can see the system. Web. Extensions Assembly we are looking for. It is also a dir, continue to switch in and Dir.

At this time, there is only one directory, continue to switch in, And then dir will be able to see the DLL file we finally want, and then copy it through the Copy command and it will be OK!

TIPS: when using commands in cmd mode, if the file name or directory name to be entered is too long, you can first knock some characters and then use the tab key to automatically complete, the Windows command tool will automatically find the matching content for you!

Vi. Example

As shown in, two new class library files are created: classlibrary1 and classlibrary2.

1. Create a strongly-typed Assembly classlibrary1 using the third point above and register it in GAC (you can use the method in the third point above, or use the anti-compiler for decompilation, you can view the publickeytoken value. Classlibrary2 is null, and classlibrary1 is 568e03e6162a7a2e ).

2. Reference classlibrary1 and classlibrary2 in dataaccess to compile dataaccess.

3. Go to the bin \ debug folder of the dataaccess project, only classlibrary2.dll and dataaccess. dll (no classlibrary1.dll)

It can be seen that the program directly obtains classlibrary1.dll from GAC, rather than copying classlibrary1.dll to its own debug for reference from the classlibrary1 project.

Http://www.cnblogs.com/smallstone/archive/2010/06/29/1767508.html

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.