The topic of Assemblies is always difficult to grasp for new developers. In this article I will explain what assembly in .NET means and what are the different types of assemblies available in the .NET framework.

Introduction:

The topic of Assemblies is always difficult to grasp for new developers. In this article I will explain what assembly in .NET means and what are the different types of assemblies available in the .NET framework.

What is an Assembly?

An assembly is a unit of deployment. Think of an assembly as a package that contains methods, fields, properties, interfaces, images and much more. All these entities exists inside an assembly in the form of IL (Intermediate Language) format and are converted to machine language at runtime through the CLR. An assembly can exists in the form of PE (Portable Executable) or a DLL (Dynamic Link Library).

Let's take a simple example where you can use an assembly. Suppose, that you want to write some methods to do arithmetic calculations. So, you created a project and you created all the methods necessary for your application and in the end you either had PE or DLL. This means you created a simple assembly file which now can be distributed to different developers. And the purpose of the assembly is to solve arithmetic problems.

Types of Assemblies:

There are two types of assemblies namely, Weakly Named Assemblies and Strongly Named Assemblies. Both the assemblies have everything in common but the Strongly Named Assembly is signed with the publisher's public/private key pair. This signing of the assembly makes the assembly unique, secured and versioned. Later, in this article I will show you that how you can sign your assemblies and put them in the GAC (Global Assembly Cache).

Please also note that only the Strongly Named Assemblies can be placed in the GAC directory.

Deployment of Assemblies:

An assembly can be deployed in two different ways namely privately and globally. Private deployment means that the assembly will reside in the bin directory of the application's folder. Global deployment means that the assembly will reside in the GAC (Global Assembly Cache). The GAC contains all the assemblies which comes with the .NET framework. You can view all the assemblies in the GAC by using your Explorer and browsing the following link: C:\WINDOWS\assembly. All the assemblies that reside in the GAC are strongly named assemblies. You cannot even a weakly named assembly in the GAC folder. Take a look at the code below in which I tried to add the weakly named assembly to the GAC folder and got the error message:

The error message "Failure adding assembly to the cache: Attempt to install an assembly without a strong name" clearly states that all the assemblies which are meant to be installed in the GAC directory must be strongly named assemblies.

Creating Strong Named Assemblies:

Let's see how we can make a strongly named assembly so that we can add it to the GAC folder. The first thing that you need to do is to make the Strong Name this can be done by using the SN.EXE tool. Simply, type the following command on your Visual Studio.NET command prompt.

SN.exe -k MyProject.keys

This will create the MyProject.keys file which will contain the private and public keys. If you are curious and want to see the public key then you can use the SN.EXE tool with a -p switch. Take a look at the line below:

SN.exe -p MyProject.keys MyProject.PublicKey

Now, to view the PublicKey you can use the following line of code:

You cannot use the SN.exe tool to view the private key. Now, to sign the assembly with the strong name you use the following line of code:

csc /keyfile:MyProject.keys Program.cs

Adding the strong named assembly to GAC:

Now, it is time to add the strong named assembly to the GAC folder. Simply, run the GACUtil.exe tool to add the assembly to the GAC folder.

GACUtil.exe -i Program.exe

Now, if you view the C:\windows\assembly folder you will realize that the new assembly (Program.exe) has been added. Once, the assembly is added to the GAC folder you can use it in multiple projects.

Conclusion:

In this article I explained the basics of the assemblies in .NET. There is much more you can do with assemblies which includes versioning, security etc. Also, it is always a good idea to use the ILDasm.exe tool to view the IL generated by the assembly. This way you will have better idea of what goes on behind the scenes.

I hope you liked the article, happy coding!