Assemblies In .NET Application

Datetime:2016-08-22 22:50:54         Topic: .Net  Assembler          Share        Original >>
Here to See The Original Article!!!

Topics that I am going to cover in this article:

  • What are Assemblies ?
  • Types of Assemblies.
  • Managing Assemblies in Global Assembly Cache.
  • Generating a Public Key.
  • Creating a Shared Assembly.
  • Versioning of Assembly.

Let’s Start

What is Assembly in .NET?

Assemblies are the core building blocks of .NET applications. In simple terms,  assembly is the .dll or .exe file that is created while building any .NET application. You can find many definitions for assembly over the internet also. In my point of view, assembly is the building block which is created after a successful build operation of your application.

In every project, after compilation, an output file is generated, known as Assembly. The name of an assembly file is the same as the project name. The assembly corresponding to a project remains present under bin\Debug folder of that project folder. Assemblies are known as units of deployment because once the project development is completed, what we carry and install on the client systems is the assembly files only. The extension of an assembly file uses to be either .exe (executable) or .dll (dynamic link library).

Windows Forms Applications, Console Applications, WPF Applications, and Windows Services projects generate an .exe assembly. Class Library and Windows Forms Control Library projects generate a .dll assembly.

Exe assemblies are known as in-process components which were capable of running on their own as well as provide the support for others to execute. When we work with project templates, like Windows Forms Applications, Console Applications, WPF Applications, and Windows Services, they generate an exe assembly when compiled.

Dll assemblies are known as out-process components which are not capable of running on their own. They can only support others to execute. When we work with project templates, like Class Library and Windows Forms Control Library, they generate a dll assembly when compiled.

Note: Every application is a blend of both .dll and .exe assemblies combined together to give better efficiency.

Types of Assembly:

In .NET, we have two types of assemblies:

  1. Private Assembly
  2. Shared Assembly

Private Assembly

By default, every assembly is private. If reference of these assemblies is added to any project, a copy of the assembly is created and given to that project, so that each project maintains a private copy of that assembly.

Creating an assembly to test if it is private, by default :

Open a new project of type Class Library and name it as “PAssembly”, which will by default come with a class Class1 under the file Class1.cs. Now, write the following code under the class:

public string TestHello() {  
return "Hello from private assembly";  

Now, compile the project by opening the Solution Explorer, right clicking on the project, and selecting “Build” which will compile and generate an assembly with the name as PAssembly.dll.

Note: We can find the path for our assembly in the output window present at the bottom of the Visual Studio after successful build.

Testing the assembly we have created

Open a new project of type Windows, name it as “TestPAssembly”, place a button on the Form, and set its text as “Call TestHello method of Class1 in PAssembly.dll”. Now, add the reference of PAssembly.dll from its physical location and write the following code under click of button:

PAssembly.Class1 obj = new PAssembly.Class1();  
MessageBox.Show(obj. TestHello ());  

Run the project to test it. Then, go and verify under bin/debug folder of current project where we can find a copy of PAssembly.dll as it is private assembly.

Note: The advantage of a private assembly is faster execution because it is available in the local folder, whereas its drawback is that multiple copies get created when multiple projects add the reference to consume it.

Shared Assemblies

If we intend to use an assembly among several applications, private assemblies are not feasible. In such cases, we can install it into a centralized location known as the Global Assembly Cache. Each computer where the common language runtime is installed, has this machine-wide code cache. The global assembly cache stores the assemblies specifically designated to be shared by several applications on the computer. All BCL assemblies are shared .dll assemblies only. So, we can find them under GAC. If an assembly is shared, multiple copies of the assembly will not be created, even if it is being consumed by multiple projects. Only a single copy, under GAC, serves all the projects.

Administrators often protect the Windows directory using an Access Control List (ACL) to control, write, and execute the access. Because the global assembly cache is installed in the Windows directory, it inherits that directory's ACL. It is recommended that only users with Administrator privilege be allowed to add or delete files from the global assembly cache.

Managing assemblies in Global Assembly Cache

To manage assemblies in GAC, like install, un-install, and view, we are provided with a tool known as Gacutil.exe (Global Assembly Cache Tool). This tool is automatically installed with Visual. To run the tool, use the Visual Studio Command Prompt.

Where is VS Command prompt ?

Go to Start menu - click on Visual Studio folder - Inside this, you find VS Command prompt.

These utilities enable you to run the tool easily, without navigating to the installation folder. To use Global Assembly Cache on your computer: On the Taskbar, click Start --> All Programs --> Visual Studio. Click Visual Studio Tools and then click Visual Studio Command Prompt and type the following:

gacutil [/i | /u | /l] <assembly name>

/i: Install

/u: Un-install

Note: Assemblies deployed in the GAC must have a strong name. When an assembly is added to the global assembly cache, integrity checks are performed on all files that make up the assembly.

What a Strong Name is ?

A strong name consists of the assembly's identity - its simple text name, version number, and public key.

  1. Name: It is the name of an assembly used for identification. Every assembly by default has a name.
  2. Version: Software maintains versions for discriminating changes that have been made from time to time. As an assembly is also a software component,   it will maintain versions. Whenever the assembly is created, it has a default version for it, i.e., which can be changed when required.
  3. Public Key: As GAC contains multiple assemblies in it, to identify each assembly, it will maintain a key value for the assembly known as public key, which should be generated by us and associated with the assembly to make it Strongly Named.

You can ensure that a name is globally unique by signing an assembly with a strong name. In particular, strong names satisfy the following requirements:

Strong names guarantee the name uniqueness by relying on unique key pairs. No one can generate the same assembly name that you can. Strong names protect the version lineage of an assembly.

A strong name can ensure that no one can produce a subsequent version of your assembly. Users can be sure that a version of the assembly they are loading comes from the same publisher who created the version the application was built with.

Strong names provide a strong integrity check. Passing the .NET Framework security checks guarantees that the contents of the assembly have not been changed since it was built.

Generating a Public Key

To sign an assembly with a strong name, you must have a public key pair. This public cryptographic key pair is used during compilation, to create a strong-named assembly. You can create a key pair using the Strong Name tool (Sn.exe) from visual studio command prompt, as the following:

sn -k <file name>

Note: The above statement generates a key value and writes it into the file “Key.snk”. Key pair files usually have .snk extension

Creating a Shared Assembly

Step 1:Generate a public key. Open VS command prompt, go into your folder and generate a public key as following: <drive>:\<folder> sn -k key.snk,

Now, my key.snk is successfully created inside Drive ‘E’.

Step 2:Develop a new project and associate the key file to it before compilation, so that the assembly which is generated will be Strong Named.

To do this, open a new project of type Class Library, name it as “SAssembly”, and write the following code under the class Class1:

public string TestHello() {  
return "Hello from shared assembly";  

To associate the key file we have generated with the project, open project properties window, select Signing tab on LHS which displays a CheckBox as “Sign the Assembly”, and select it. Now, in the ComboBox below select browse, select the key.snk file from its physical location which adds the file under Solution Explorer. Then, compile the project using “Build” option that will generate SAssembly.dll which is Strongly Named.

Now, browse your folder and choose the key.snk file.

Click OK. Then, the key shows you inside the Visual studio.

Now, build the project; and it’s ready with the dll with key inside your Bin\debug folder .

Step 3:Install assembly into GAC by using the “Global Assembly Cache Tool”.

To install the assembly, open Visual Studio Command Prompt, go to the location where SAssembly.dll is present, and write the following:

Again, open this in Admin mode and run the above command.

Step 4: Testing the Shared Assembly.

Open a new project of type Windows, name it as “TestSAssembly”, place a button on the form, and set its text as “Call SayHello method of Class1 in SAssembly.dll”.

Now, add reference of SAssembly.dll from its physical location and write the following code under the click of button:

SAssembly.Class1 obj = new SAssembly.Class1();  

Run the project, test it, and verify under bin/debug folder of current project where we will not find the only copy of SAssembly.dll as it is shared assembly.

Check inside project Bin/debug folder.

Versioning Assemblies

Every assembly is associated with a set of attributes that describe about general info of an assembly, like Title, Company, Description, Version etc. These attributes are under AssemblyInfo.cs file of each project. To view them, expand properties node under the project in Solution Explorer where we find AssemblyInfo.cs file. We can change values of any attribute as per our requirements. In bottom of the file, we find an attribute “AssemblyVersion”.
which is a combination of 4 values,
  • Major Version
  • Minor Version
  • Build Number
  • Revision

The default version of every assembly is; we can change the version no. of an assembly whenever we make modifications to the code under assembly.

This is all about .NET Assembly and their uses.

So, I think I have covered all the topics successfully.