Reprint using the GAC in. Net

Source: Internet
Author: User

Reprint Source https://blog.log4d.com/2011/01/gac/

Gac

What is the GAC? What do you do for a while? The GAC's full name is called the global assembly cache, and popular understanding is the place to store the DLLs that are needed under various. NET platforms. The specific catalog of the GAC is in windows/assembly.

Friends who like to use cracked software may often use the replacement DLL, although this DLL differs from the. NET DLL, but the general effect is the same: use a common dynamic-link library to improve library reuse and reduce file volume. Before the. NET platform, the programs put public DLLs that might be used into the System32 folder. Due to version inconsistencies and other reasons (storing control information in the registry), a well-known DLL Hell is created.

Recently reading Shiki's "self-cultivation of programmers," the middle of a large space to describe the process of compiling links in the program. Coincidentally, my C # and. NET 3.5 Advanced Programming also read the assembly. Microsoft used the GAC to avoid repeating the DLL. Now, I'm going to deploy a simple generic class library into the GAC.

Get Strong name (signature)

Not just a DLL can be put into the GAC, if so, and the previous DLL hell is no different. The GAC uses a strong name (signature) to differentiate different DLLs. Each signature is a unique public-private key pair. Now we use the SN tool of the SDK to generate an SNK signature for my Dutil library.

Open the Visual Studio 2008 command Prompt in the SDK, type Sn-k dutil.snk, and the results are as follows:

F: WorkSpace. Netdutilbindebug>sn-k dutil.snk

Microsoft (R). NET Framework Strong Name Utility version 3.5.21022.8

Copyright (c) Microsoft Corporation. All rights reserved.

The key pair is written to Dutil.snk

So we get an SNK signature file.

To set a signature for an assembly

Now we have a signature file, but we haven't applied the signature to the program, and added [Assembly:assemblykeyfile ("Dutil.snk")] to the program's Assembly.cs file. This dutil.snk can be an absolute path, I am using the SNK file in the home directory.

Compile the solution, and the generated. dll file contains the signature.

At this point, a warning appears in the compiler:

Warning 1 Replace "AssemblyKeyFile" with the command-line option "/keyfile" or the appropriate project settings F:workspace.netdutilpropertiesassemblyinfo.cs Dutil

Since vs suggests we use option settings, we'll use the Project Options page to set the key. As shown in the following:

PS: I tested, after generating the DLL, even if the signature used to delete the SNK file, can still be used normally. That is, the SNK file is used only at compile time.

Installing to the GAC

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

Under Win7, if you drag, you will encounter permissions problems, as shown in

The following error will also be encountered under Gacutil

F:workspace.netdutilbindebug>gacutil-i Dutil.dllmicrosoft (R). NET Global Assembly Cache Utility.   3.5. 21022.8 Copyright (c) Microsoft Corporation.  All rights reserved.

Adding an assembly to the cache failed: Access denied. You may not have administrative credentials to perform this task. Please contact your system with the tube

Contact the administrator for assistance.

The solution is to enter the command line as an administrator, after execution is displayed as follows

F:workspace.netdutilbindebug>gacutil-i Dutil.dllmicrosoft (R). NET Global Assembly Cache Utility.   3.5. 21022.8 Copyright (c) Microsoft Corporation.  All rights reserved.

The assembly was successfully added to the cache

When you look at the windows/assembly directory, you will find that Dutil is already inside.

Open windows/assembly with Totalcommander, you will find Gac/gac_32/gac_msil these folders, my Dutil installed to C:windowsassemblygac_ Msildutil.1.0.0__35f4c1ba225b3cc6dutil.dll, this path contains the version, the signature, in this way, you can avoid the issue of version and wrong DLL.

DLLs using the GAC

Open any project, add references, select the compiled DUtil.dll. Looking at the specific properties, you will find that the DLL is not copied locally because it is available from the GAC.

If we remove the DLL from the referenced place, we will find that the program can still run correctly, stating that our DLL is actually from the GAC. (Update: When referencing a DLL file, it is preferred to refer to this DLL, which does not exist to reference the GAC DLL)

PS: Do not try to open the windows/assembly directory to reference, you will find you in vain.

Uninstalling DLLs in the GAC

A word.

C:windowssystem32>gacutil-u dutilmicrosoft (R). NET Global Assembly Cache Utility.   3.5. 21022.8 Copyright (c) Microsoft Corporation.  All rights reserved.

Assemblies: Dutil, version=0.1.0.0, Culture=neutral, PUBLICKEYTOKEN=35F4C1BA225B3CC6

, Processorarchitecture=msil

Uninstalled: Dutil, version=0.1.0.0, Culture=neutral, PUBLICKEYTOKEN=35F4C1BA225B3CC6

, Processorarchitecture=msil

Number of unloaded assemblies = 1

Number of failures = 0

This will unload the DLL for the specific target.

Reference articles

In addition to the two books mentioned above, reference is made to the 815808 articles in MSDN, article name how to install assemblies into the global assembly cache in Visual C #. NET.

Reprint using the GAC in. Net

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.