Use GAC in. net

Source: Internet
Author: User
Use gacgac in. net

What is GAC? Why? GAC is short for Global Assembly Cache. A common understanding is to store the DLL required for various. Net platforms. The specific directory of GAC is in Windows/assembly.

Friends who like to use the cracking software may often use the replacement DLL method, although this DLL and.. Net DLL is different, but it generally plays the same role: Use a common dynamic link library to improve library reuse and reduce file volume. Before the. NET platform, the program stored the public DLL that may be used in the System32 folder. Due to version inconsistency and other reasons (storing control information in the Registry), a famous DLL hell is created.

I recently read Yu's "programmer's self-cultivation", and spent a lot of time describing the link in program compilation. Coincidentally, my C # And. Net 3.5 advanced programming just read the assembly. Microsoft uses GAC to avoid repeated DLL errors. Now, I will deploy a simple generic class library to GAC.

Obtain a strong name (Signature)

Not any dll can be put in GAC. If so, it is no different from the previous DLL hell. GAC uses a strong name (Signature) to distinguish between different DLL. Each signature is a unique private key pair. Now we use the SDK's Sn tool to generate an SNK signature for my dutil library.

Open the Visual Studio 2008 command prompt in the SDK and type Sn-K dutil. SNK. The result is as follows:

View Source

Print?

1 F:\WorkSpace\.net\DUtil\bin\Debug>sn -k DUtil.snk
2   
3 Microsoft (R). Net Framework utility version 3.5.21022.8
4 Copyright (c) Microsoft Corporation.  All rights reserved.
5   
6 The key pair is written into dutil. SNK.

In this way, we get an SNK signature file.

Set signature for assembly

Now we get a signature file, but we haven't applied this signature to the program, in the assembly of the program. add [Assembly: assemblykeyfile ("dutil. SNK ")], this dutil. SNK can be an absolute path. I use the SNK file in the main directory.

Compile the solution. The generated. dll file contains the signature.

The compiler generates a warning:

Warning 1 use the command line option "/Keyfile" or appropriate project settings to replace "assemblykeyfile" F: \ workspace \. Net \ dutil \ properties \ assemblyinfo. CS 38 12 dutil

Since vs recommends that you use option settings, you can use the project option page to set the key. As shown in:

PS: I tested it. After the DLL is generated, even if the SNK file used by the signature is deleted, it can still be used normally. That is, the SNK file is only used during compilation.

Install to GAC

There are two ways to install GAC: one is to drag the DLL into the windows/Assembly directory (not copy and paste, or drag), and the other is to use gacutil.

In win7, you may encounter permission problems when dragging, as shown in

The following error occurs in gacutil:

View Source

Print?

1 F:\WorkSpace\.net\DUtil\bin\Debug>gacutil -i DUtil.dll
2 Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.21022.8
3 Copyright (c) Microsoft Corporation.  All rights reserved.
4   
5 Failed to add assembly to cache: Access denied. You may not have any management creden。 to execute this task. Contact your system
6 Contact the Administrator for help.

The solution is to enter the command line as an administrator. After the command is executed, the following information is displayed:

View Source

Print?

1 F:\WorkSpace\.net\DUtil\bin\Debug>gacutil -i DUtil.dll
2 Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.21022.8
3 Copyright (c) Microsoft Corporation.  All rights reserved.
4   
5 The Assembly has been added to the cache.

Check the windows/Assembly directory and you will find that dutil is already in it.

Open Windows/assembly with totalcommander and you will find the GAC/gac_32/gac_msil folders in it. install my dutil in C: \ windows \ Assembly \ gac_msil \ dutil \ 0.1.0.0 _ 35f4c1ba225b3cc6 \ dutil. DLL. This path contains the version and signature. In this way, you can avoid version and incorrect DLL problems.

Use GAC DLL

Open any project and select the compiled dutil. dll when adding a reference. Check the specific properties and you will find that the DLL is not copied to the local machine because it can be obtained from GAC.

At this time, if we delete the DLL from the reference place, we will find that the program can still run correctly, indicating that our DLL is indeed from GAC. (Update: When the referenced DLL file exists, the DLL is referenced first. If the DLL does not exist, the gac dll is referenced)

PS: do not try to open the windows/Assembly directory for reference. You will find that you are in vain.

Uninstall the DLL in GAC

One sentence

View Source

Print?

01 C:\Windows\system32>gacutil -u DUtil
02 Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.21022.8
03 Copyright (c) Microsoft Corporation.  All rights reserved.
04   
05 Assembly: dutil, version = 0.1.0.0, culture = neutral, publickeytoken = 35f4c1ba225b3cc6
06 , processorArchitecture=MSIL
07 Uninstalled: dutil, version = 0.1.0.0, culture = neutral, publickeytoken = 35f4c1ba225b3cc6
08 , processorArchitecture=MSIL
09 Number of uninstalled assemblies = 1
10 Failure COUNT = 0

In this way, the DLL of the specific target is uninstalled.

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.