An assembly is the primary building block of the Microsoft
.NET Framework. This article discusses assemblies, their types and deployment.
What are assemblies?
An Assembly is the smallest unit of versioning, security,
deployment, version control and reusability of code in Microsoft. NET Framework.
An assembly can contain one or more files. Such assemblies can be Single file
or Multi file assemblies. A single file assembly contains everything packed
together in a single file. A multi file assembly however, contains a number of
.NET modules and is used for large scale applications.
Contents of an Assembly
Assemblies contain the following
·
Assembly Identity – its name, version information, etc.
·
Manifest
·
Metadata
·
MSIL code
·
Type information
·
Security information
·
Resources
Why do we use assemblies?
The fundamental unit of deployment in the Microsoft .NET Framework
is assemblies. The benefits of assemblies include but are not restricted to:
·
Versioning
·
Type identification
·
Security
·
Deployment
·
Referencing
Types of Assemblies
Based on their accessibility, assemblies can be of the
following types
·
Private
·
Shared
Private Assemblies
Private assemblies are those that are used by a single
application. The private assemblies are housed in the application folder. They
can be accessed by the same application domain only. The assemblies that we
create are by default private unless we take the necessary measures to make an
assembly shared.
Shared Assemblies
Shared assemblies are ones that are accessed by multiple
applications. The shared or global assemblies have globally unique names and
are stored in the Global Assembly Cache (GAC).
The following are the benefits of a shared assembly:
·
Reusability
·
Version control
Static and Dynamic Assemblies
Assemblies can be static or dynamic. Static assemblies are
stored in the disk as physical files but dynamic assemblies are not saved to
disk before execution and are executed directly from memory.
Metadata
Metadata refers to information about an assembly and is
stored in the portable executable file in a language neutral manner at the time
of compilation. An assembly Metadata consists of the following:
·
Identity information
·
Culture information
·
Type description
·
Dependency information
·
Security Information
·
Attributes
Manifest
Assembly metadata is also referred to as the manifest. The
manifest is a set of metadata that contains information about the assembly
itself. This information includes:
·
The name of the assembly
·
The version of the assembly
·
Culture information
·
A list of files in the assembly
·
Type information
·
Reference information
Versioning
An assembly consists of a 128 bit version number that is
divided into the following four parts:
·
Major
·
Minor
·
Build
·
Revision
Hence, an assembly having a version as 1.0.1.1 has 1 as the
major version, 0 as the minor version, 1 as the build and 1 as the release.
The Global Assembly Cache (GAC)
This is the central storage of shared assemblies that are
identified by having globally unique identifiers.
Assemblies that are to be deployed in the global assembly
cache must have unique identifiers. This unique identification is provided by a
strong name. Such assemblies are also called Strong Named Assemblies.
What is in a strong name?
A strong name comprises the following:
·
The assembly’s identity
·
A public key
·
A digital signature
A strong name is
typically generated using the sn.exe tool, described below.
Assembly tools
This section deals with some important tools related to
assemblies like:
·
ilasm
·
ildasm
·
sn
·
al
·
gacutil
Assembler & Disassembler
The utility ilasm is the assembler utility. The ildasm.exe
is the disassembler and is the utility that is used to inspect the internals of
an assembly. Each of these utility's names begins with "IL" because
they deal with Intermediate Language (also referred to as MSIL or Microsoft
Intermediate Language), the language used for all code within .NET assemblies.
Strong Name Utility
This utility is used to create a strong name for an
assembly. This utility is used to create a file called the strong name key file
that contains the public and private keys that an application would use. The
strong name utility is used to create:
·
A unique strong name
·
Public or private keys
The following is the command to create a strong name:
This would create an output file called test.snk, a strong
named key file.
Signing an assembly
In order to sign the assembly, i.e, add the key we add the
following in the file AssemblyInfo.cs file:
<assembly:AssemblyKeyFile("c:\\test.snk")>
Visual Studio can also be configured to sign an assembly
through project properties, or you may be
able to use ILASM and ILDASM.
Assembly linker
In order to share an assembly, the assembly linker utility
al is used. This utility would install the assembly in the global assembly
cache. This utility automatically creates the proper folder in the global
assembly cache. The following is the command to use the utility al to store the
assembly in the global assembly cache such that it is shared across
applications:
Global Assembly Cache Utility
This utility is used to store an assembly in the global
assembly cache and makes it globally shared and hence globally available for
applications. The following is the command used with the gacutil utility to
make the assembly test.dll globally accessible.
Contrary to the popular belief a strong named assembly can
also be placed elsewhere other than the GAC, like the application’s directory
and still made globally accessible, but, it should then be deployed using the
XCOPY command.
Assembly deployment
The private assemblies do not require any strong name. They
reside in the same application folder. The shared assemblies require a strong
name for identification. Multiple versions of shared assemblies can exist
together, and each may be referenced individually, unlike private assemblies in
a single application folder. Developing and deploying a shared assembly is
discussed in the following section.
Installing an assembly in the GAC
The following section uses what we have learnt so far and
discusses installation of an assembly into the GAC.
Create a key pair using the sn utility.
At the command prompt, type the following:
This would create a file called test.snk containing the key
pair.
Compile the project to create a dll.
Use the gacutil utility to install the assembly in the GAC.
Sign the assembly by using the following (use the actual
path to your key file):
<assembly: AssemblyKeyFile("c:\\test.snk")>
Viewing the GAC
To view the contents of the global assembly cache, use the
following command at the command prompt:
Using the /r option as shown below would display the
referenced assemblies also.
You can also view the GAC in Windows Explorer by navigating
to your WINDOWS\assembly folder on your system drive.
Removing an assembly from the GAC
In order to remove an assembly from the GAC, use the
following:
This would remove the assembly test.dll from the GAC.
Suggested Readings
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconassemblymanifest.asp
http://www.expresscomputeronline.com/20040913/techspace02.shtml
http://www.codeguru.com/csharp/csharp/cs_syntax/anandctutorials/article.php/c5845/
http://www.c-sharpcorner.com/UploadFile/crausch/Assemblies-TheInsandOutI12222005230030PM/Assemblies-TheInsandOutI.aspx?ArticleID=1540292a-2e6f-4b4d-8b14-407070ddfbd0
Conclusion
This article has provided a bird’s eye view of the concepts
of assemblies, their types and deployment. Please feel free to post your
comments and suggestions if any.