You are on page 1of 18

Ravi Gaurav Pandey    ravig.pandey@gmail.

com 

Inside .net Assmblies 
Background

Assembly: According to MSDN “Assemblies are the building blocks of .NET


Framework applications; they form the fundamental unit of deployment,
version control, reuse, activation scoping, and security permissions. An
assembly is a collection of types and resources that are built to work
together and form a logical unit of functionality. An assembly provides the
common language runtime with the information it needs to be aware of type
implementations. “

There are two types of assembly

• Private: The assembly which is used only by a single application is


called as private assembly.It does not require strong name and
versioning.
• Shared: Assembly which can be used across multiple applications is
called shared assembly.

Click here to find more details about assembly.

GAC (Global assembly cache): GAC is a place where .NET assemblies are
stored, specifically used to be shared by multiple applications on that
computer.

GACUtil is a command line tool which allows you to place, to remove


assembly from GAC.

To install an assembly called MyAssembly in the GAC, you can use the
command
gacutil /i MyAssembly.dll

To uninstall an assembly called MyAssembly in the GAC, you can use the
command,

gacutil /u MyAssembly.dll

  Page 1 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

1. CREATING A .NET ASSEMBLY

To create a Component or Assembly we have to create a Class Library


project by using Visual Studio .NET.

Step 1 : Open Visual Studio .NET

Create a new Class Library project in Visual Studio .NET. Develop code for
your class without any “public static void Main(string[] args)” and press F6
or Shift+F6 to build a dll file of your code.

Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe

Now, we will use the SN tool to generate the cryptographic key pair, which is
located in the \bin subdirectory, where the .NET Framework Solution
Developer Kit (SDK) is installed. We will open the DOS prompt. The
command-line statement format is:

sn -k "C:\[DirectoryToPlaceKey]\[KeyName].key"

Next, create a directory named mykeys in C:\ so that you can easily locate
the key and access the key from the command prompt.

NOTE:

For most users, the .NET tools are located in C:\Program


Files\Microsoft.NET\FrameworkSDK\Bin.

Type the following at the command prompt.

sn -k C:\mykeys\mykeys.key

Step 3 : Sign the component with the key

A key is generated, but it is not yet associated with the project's assembly.
To establish the association, double-click the AssemblyInfo.vb or
AssemblyInfo.cs file in the Visual Studio Solution Explorer. Add the following

  Page 2 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

to the list of assembly attributes that are included in this file by default when
a project is created in Visual Studio .NET IDE

<Assembly: AssemblyKeyFile("C:\mykeys\mykeys.key")>

Now recompile the project and the assembly will be signed.

Step 4 : Host the signed assembly in Global Assembly Cache


There are two ways to install an assembly into the global assembly cache:

Using Microsoft Windows Installer 2.0


This is the recommended and most common way to add assemblies to the
global assembly cache. The installer provides reference counting of
assemblies in the global assembly cache, plus other benefits.

Using the Global Assembly Cache tool (Gacutil.exe)


You can use Gacutil.exe to add strong-named assemblies to the global
assembly cache and to view the contents of the global assembly cache.

Note:

Gacutil.exe is only for development purposes and should not be used to


install production assemblies into the global assembly cache.

Syntax to install a strong-named assembly into the global assembly cache

You can install the .dll file by using the Gacutil Tool or by dragging the .dll
file into the appropriate directory. If you use the Gacutil Tool, you can use
the following command:

At the command prompt, type the following command:

gacutil I <assembly name>


In this command, assembly name is the name of the assembly to install in
the global assembly cache.

The following example installs an assembly with the file name myGAC.dll
into the global assembly cache.

  Page 3 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

gacutil -I "C:\[PathToBinDirectoryInVSProject]\myGAC.dll"

After hosting the assembly just go to WINNT\Assembly folder and you will
find your assembly listed there.

Step 5 : Test the assembly

Once you've installed the assembly into the GAC, you can use it from other
programs by creating a reference.

Expand the project and then right-click on the References item. Select "Add
Reference" from the popup menu, and the Add Reference dialog box is
displayed.

To refer an assembly in the GAC, just click on the Browse button and browse
to the directory (myGAC/bin/debug) that contains the assembly
(myGAC.dll). Locate the assembly, select it, and click OK.

  Page 4 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

2. THE ASSEMBLY “ADD REFERENCE” DIALOG BOX

The Add Reference dialog box displays tabbed panes that list the various types of
components and projects you can browse. To view the Add Reference dialog box, when
a project is selected, click Add Reference on the Project menu, or right-click the
Reference node in Solution Explorer and click Add Reference. In the Add Reference
dialog box, select the tab for the type of component you wish to browse.

Note:

The number of tabs available at the top of the Add Reference dialog box can vary,
depending on the type of project open and the resources it is using. Also some
components in the list may be disabled and display in grey depending on the .NET
Framework version of your project. This is because some components from a more
recent .NET Framework version are not compatible with projects based on an earlier
.NET Framework version.

Changing the Add Reference dialog box edits the project references file for the
selected project. A project references file contains information about the run-time
requirements of an application or component, such as which files are needed, how they
are to be registered, and where on the user's machine they should be installed. The Add
Reference dialog box also adds the object hierarchies for the selected projects to the
browsing scope in Class View and the Object Browser.

Security Note:

When incorporating components that require full-trust permissions to run, be aware


that code-access security violations could occur. During development, a project runs at
full-trust, but the environment the project is deployed to might be partial-trust. At run
time, if the environment is partial-trust, you can get code-access security violations.
Thus, it is necessary to test applications in a variety of diverse 'trust' scenarios.

  Page 5 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

The following tabs and controls are available:

.NET Tab

Lists all .NET Framework components available for referencing.

Component Description

Component Either the full or "friendly" name of the component.


Name

Version The version number of the component.

Runtime The version number of the .NET Framework that the component
was created with.

Path The folder path and filename of the component.

COM Tab

Lists all COM components available for referencing.

Note:

This tab does not appear during development of Smart Device projects.

Component Description

Component Name Either the full or "friendly" name of the component.

TypeLib Version The version number of the component.

Path The folder path and filename of the component.

Projects Tab

  Page 6 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Lists Visual Studio projects in the current solution available for referencing. Select
assemblies from this tab to create project-to-project references.

Component Description

Project Name Displays the names of referenced projects.

Project Directory Displays the folder path for referenced projects.

Browse Tab

Allows you browse additional files to find a component not listed in the current
tab and add it to the list.

Recent Tab

Displays recently added references.

Component Description

Component Either the full or "friendly" name of the component.


Name

Type The component type listed.

Version The version number of the component.

Source The file name of the component and the path to the folder where it
is located.

Displaying an Assembly in the Add Reference Dialog Box

The Add Reference dialog box does not automatically display every assembly, even if
an assembly has been installed to the Global Assembly Cache (GAC). The Add
Reference dialog box is path-based, and there are several methods to display an
assembly:

  Page 7 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

• Move or copy the assembly to the current project directory (you can find these
assemblies using the Browse tab), other project directories within the same
solution (you can find these assemblies using the Projects tab), or the Public
Assemblies folder at Program Files\Microsoft Visual Studio
.NET\Common7\IDE\Public Assemblies; (you can find these assemblies on the
.NET tab).
• Set a reference path to the directory containing the assembly using the Reference
Paths Dialog Box (Visual Basic) or the Reference Paths Page, Project Designer
(C#).
• Set a registry key that specifies the location of assemblies to display.

For detailed procedures on how to display an assembly in the Add Reference dialog
box, see How to: Add or Remove References in Visual Studio (Visual Basic).

References List Depends On .NET Framework Target

The reference components displayed in the Add Reference dialog box is tied to the
version of the .NET Framework used in the project. Components that are not applicable
to the project's .NET Framework target display in grey in the list and cannot be added as
a reference to the project.

   

  Page 8 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

3. The abstract view of “Windows/Assembly” folder

Have you ever tried to copy DLL from GAC (Global assembly cache)? Well,
first time when someone asked me, I said go to GAC
(c:\Windows\Assembly) folder, Select the assembly you want to copy, then
right click on it and select copy option and paste it at your desired location.
Well, I tried the same way but unfortunately there is no option available to
copy when you make a right click on any assembly. Only available options
are uninstall and properties option.
See the below screen shot.

One more thing that is noticeable is, go to DOS prompt and fire DIR
command to see the listing of C:\Windows\Assembly folder and you will be
surprised to see the listing. See below screen shot.

  Page 9 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Where C:\Windows\Assembly folder looks like this.See Screenshot below:

Surprised!! Well, what you see on the DOS Prompt is the internal structure
of GAC folder then why windows is not showing such structure of the GAC.
  Page 10 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Well, this is because off SHFusion.dll (Assembly cache Viewer). On the Dos
Prompt fire Dir /AH command. It shows desktop.ini. SHFusion.dll uses this
desktop.ini file to show abstract view of GAC.

Various Techniques to show internal structure of GAC

There are 4 ways to show the same structure for GAC in windows as we will
in DOS.

Rename the Desktop.ini file

As I mentioned previously, SHFusion.dll make use of desktop.ini to


determine how to display the content of the GAC folder. From DOS prompt,
fire these commands to rename the desktop.ini file.

attrib desktop.ini -h -r -s
rename desktop.ini desktop.ini.bak

Now, go to C:\Windows\Assembly folder to see its content. Screen will look


something like below screenshot.

  Page 11 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

You can also see the particular folder content. Select GAC and you will see
something like this.

If you want to see both the view together run this series of commands on
DOS Prompt.

Assuming you are on c:\ drive.

cd Windows\Assembly
attrib -r -h -s desktop.ini
mkdir OriginalView
move desktop.ini OriginalView
attrib +s OriginalView
attrib +r +h +s OriginalView/desktop.ini

Now, go to assembly folder. You will internal structure of GAC and plus one
more folder named OriginalView. When you go in this folder, you will see
original view of GAC.

By modifying the registry

  Page 12 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

The following steps will modify the registry. If you make any incorrect entry
in registry, that can cause some serious problems. Sometimes you may need
to install operating system again. Use registry editor at your own risk. I
prefer, before you follow these steps, take a backup of registry.We need to
add a key in registry that will disable the abstract view of the GAC.

To open registry editor, Go to Run and type regedit. Locate following


registry, in the registry editor

"HKEY_Local_Machine\Software\Microsoft\Fusion\"

Right click on Fusion Folder and select New ->DWord Value.

  Page 13 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Add a new Dword named “DisableCacheViewer” and set its value 1.

Now go to C:\Windows\Assembly folder and you will see folders in GAC.

By Uninstalling SHFusion.dll

Go to Visual Studio Command Prompt and fire this command to uninstall the
SHFusion.dll

regsvr32 -u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

  Page 14 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Following message will appear.

Now go to C:\Windows\Assembly folder and you will see folders in GAC.


To get back to the previous state of view register the SHfusion.dll using the
following command, fire this command on Visual Studio Command prompt.

regsvr32 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

And you will see following message on the screen.

  Page 15 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Now go to Assembly folder again and it will show the abstract view of GAC.

Using SUBST Command

Go to Windows DOS Prompt and type following command and press enter.

SUBST L: “C:\Windows\Assembly”

This command will create a virtual drive “L” and this drive will have the
internal view of GAC, where C:\Windows\Assembly will have the abstract
view of GAC. Kindly ensure that the drive name that you type in SUBST
command, it must not exist in your system. Go to My Computer and you will
see the Drive named “L:”.

Now to delete this drive, run this command on command prompt.

SUBST L: /D
This will delete the L: drive.

Folder Structure

By all above these four techniques, you can see the internal structure of
GAC. Via Internal structure you can copy the DLL and paste it at desired
location.
Now, let’s see what every folder contains in GAC. Mainly there are 5
Folders.

1. GAC : This folder contains non-native images of DLL used in .NET


Framework 1.x.
2. GAC_32 : A 32-bit system will only have the GAC_32 directory. A 64-
bit system will have both the directory GAC_32 and GAC_64. These
directories contain assemblies that are specific to 32-64 bit mode.

  Page 16 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

3. GAC_MSIL: The GAC_MSIL cache contains assemblies that can be run


in either 32-bit or 64-bit mode. They don’t have any dependency.
4. NativeImage Framework Version : Native image generated for
Framework version. If you have .NET Framework 1.0 and 2.0 both,
then there will be two directories.
5. Temporary and Tmp : Temporary Directories.

The folder GAC, GAC_32, GAC_64 and GAC_MSIL contains non-native


images of the DLLs. They all contain the MSIL that will be complied into
native images and placed in NativeImage_Framework Version folder.

  Page 17 

 
Ravi Gaurav Pandey    ravig.pandey@gmail.com 

Reference

demystifygac.aspx

http://blogs.msdn.com/junfeng/archive/2004/09/12/228635.aspx

http://msdn2.microsoft.com/en-us/library/ftcwa60a.aspx
 

  Page 18 

You might also like