Add the Assembly to GAC and enable it to be referenced in the. NET tab for adding references to.

Source: Internet
Author: User
Tags password protection

GAC is a code cache and can be used as a central knowledge base to place an assembly.

Place the Assembly in GAC so that they can be referenced by multiple applications on the server. If developers are given full version control, it is easier to manage the assembly of each application in their working directory. If you reference an assembly from GAC, every application that references the Assembly will be affected when the Assembly is updated. At first glance, this is a good automatic function. However, remember that this means that the application is updated, and this update is not tested to ensure that it will not cause damage to the application.

To add an assembly to GAC, you must give it a strong name (strong name ). If an assembly is signed with a strong name, you can ensure that the Assembly is unique. Others cannot create the same or updated assembly. This ensures that the Assembly comes from the originator and is not modified by others. The first thing to do when you name an assembly is to create a key pair that will be placed in the file.

 

There are two methods to create a key file:

  • Use attribute settings in Visual Studio 2005 project
  • Manually use the SN utility in. NET Framework

You can find the SN utility in C: \ Program Files \ Microsoft. NET \ SDK \ v2.0 \ Bin. You can also select Start | All Programs | Microsoft Visual Studio 2005 | Visual Studio Tools | Visual Studio 2005 | Command Prompt. In the command prompt, enter sn-k "C: \ MyKeyFile. snk ". This will build a strong name key file in the C: \ root directory, as shown in 2-6.

Figure 2-6 use the SN utility at a command prompt

When you use a strong name to sign an assembly using the attributes of a class library project, you must specify a strong name file. This can be done by selecting Project | Properties from the toolbar. In the displayed Properties window, select the Signing tab and select the Sign the assembly check box, as shown in Figure 2-7. Use the drop-down menu and select Browse to find the created key file to create a strong-name key file. If you have not used the SN utility to create a key file, you can select New (rather than Browse) from the drop-down menu to create the key file.


Figure 2-7 use the Signing tab in the Properties window

As shown in Figure 2-8, Visual Studio 2005 makes it easier to create new keys and provides password protection options.

Figure 2-8 Use Visual Studio 2005 to create a strong-name key

In the Properties window, select the Compile tab and set configuration to Active (Release ). This removes any debugging information and optimizes the Assembly for production. If it is not set to Release, the Code cannot be sent to production, or the code will run very slowly. You can select Build | Build Solution from the toolbar to Build a project. Note that you need to build the output path because it is required when adding the Assembly to GAC.

 

Add an assembly to GAC

To add an Assembly to GAC, you can use the Global Assembly Cache Tool (Gacutil.exe) in C: \ Program Files \ Microsoft. NET \ SDK \ v2.0 \ Bin ). You can also use Start | All Programs | Microsoft Visual Studio 2005 | Visual Studio Tools | Visual Studio 2005 Command Prompt to access the path of the tool. Remember, if you use a different. NET version, the path will be different. In the command prompt, enter gacutil.exe/I "<assembly path>" to start and use the utility, as shown in 2-9.

Figure 2-9 run the command to start gacutil.exe

Now we have added the Assembly to GAC. When you reference the assembly, you will find that the Assembly is a global Assembly after the reference, during compilation, VS will not copy it to the local directory of the project.

 

In the "add reference" dialog box of VS, the. NET tab displays the GAC assembly.

. Net gac assembly is stored in the directory C: \ WINDOWS \ assembly \ GAC_MSIL by default, but you will find that even if you put the GAC assembly in this directory, add reference in. the Assembly is still invisible in the NET tab list.

 

Here we will introduce two methods to solve this problem.

 

Solution 1: The Assembly is displayed in the "add reference" dialog box.
Even if you have installed the assembly in the Global Assembly Cache (GAC), The add reference dialog box does not automatically display each assembly. The add reference dialog box is based on the path and requires that the registry key be added. This option specifies the location of the assembly to be displayed.

The "add reference" dialog box displays the Assembly
You need to add one of the following registry items. <AssemblyLocation> is the Assembly directory to be displayed in the "add reference" dialog box, for example, C: \ MyAssemblies.

[HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \. NETFramework \ <version> \ AssemblyFoldersEx \ MyAssemblies] @ = "<AssemblyLocation>"

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \. NETFramework \ <version> \ AssemblyFoldersEx \ MyAssemblies] @ = "<AssemblyLocation>"

Note:
After you create a registry entry in the HKEY_LOCAL_MACHINE Configuration unit, all users can view the assembly at the specified location in the Add reference dialog box. Creating a registry entry in the HKEY_CURRENT_USER Configuration unit only affects the settings of the current user.
 

Restart Visual Studio.
 

 

Solution 2: You can copy your DLL. NET Framework directory (% WINDIR % \ Microsoft. NET \ Framework \ v2.x. yyyy. you can achieve your goal. however, MS suggests that we do not copy our assembly to the Framework directory. A better way is to put them in a separate directory, and then add a key to Windows Registry to tell VS. NET where these assemblies can be found. In addition to the core Framework assembly, VS. NET can also display any assembly: HKEY_CURRENT_USER \ Software \ Microsoft \. NETFramework \ AssemblyFolders In the directories listed in the following Registry keys
HKEY_LOCAL_MACHINE \ Software \ Microsoft \. NETFramework \ AssemblyFolders
HKEY_CURRENT_USER \ Software \ Microsoft \ VisualStudio \ 7.1 \ AssemblyFolders
HKEY_LOCAL_MACHINE \ Software \ Microsoft \ VisualStudio \ 7.1 \ AssemblyFolders

 

PS: Finally, the experiment found that even if the Assembly does not require a key signature and is added to GAC, add the path of the directory where the Assembly is located to the registry according to the above method, this Assembly will still appear in the VS "add reference" dialog box.. NET tab. However, when this tab references this Assembly, the Assembly is a private assembly. during compilation, VS copies the assembly to the local directory of the project.

 

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.