Understanding Assemblies
page 1 of 1
Published: 30 Mar 2006
Unedited - Community Contributed
Abstract
Assemblies form the fundamental unit of deployment, version control, reuse and security permissions for a .NET application. In this article, Joydip discusses Assemblies, types of Assemblies, Metadata and Manifest.
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 10601/ 10

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:

sn –k test.snk

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:

al /install:test.dll

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.

gacutil /i test.dll

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:

sn -k test.snk

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.

gacutil /i test.dll

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:

gacutil /l

Using the /r option as shown below would display the referenced assemblies also.

gacutil /r /l

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:

gacutil /u test.dll

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.



User Comments

Title: good   
Name: ramakrishna y
Date: 2006-12-18 10:40:32 AM
Comment:
its good but i think need some more information about assemblies, how it works in some particular tasks.
Title: Understanding Assemblies   
Name: What are assemblies?
Date: 2006-05-13 7:02:52 AM
Comment:
Overall the content is good.
but i want to understand what is Single file or Multi file assemblies. It will be better if you can explain.

My question is how an assembly can contain file?
What do you mean by file in this contest?
My understanding is an assembly is a class file(As you said in this article), where we write all our code.
Then what is the point of single or multiple file.
Title: Good   
Name: Santhakumar.P
Date: 2006-04-15 8:09:45 AM
Comment:
This article also good.it's covered all the topics in assembly but not detailed.it should understand what is what only.
Title: Topic On assemblies   
Name: Nivedita
Date: 2006-04-08 1:54:09 AM
Comment:
Well The topic on assemblies was very beautifully covered and that too in a simple way so that it can be easily understood by any one.
In the same manner the other topics of ASP.NET should also be covered so that I can have a grip on this Technology.Theoretically I have no problem but when I have to go for coding then I face problem .How to improve my coding part .Do give me suggestions.My email id is -nivedita_shankar1980@indiatimes.com
Title: Good   
Name: Venkatesa Prasath From Coimbatore,Tamil Nadu
Date: 2006-04-05 1:23:50 PM
Comment:
Very Nice. keep it up. Thanks & All the Best.
Title: Good   
Name: Ravi
Date: 2006-03-31 12:29:53 AM
Comment:
The article is very good but there is no mention of satellite assembly.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-17 9:58:37 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search