Assembly in .NET application

In the Microsoft .Net Framework, Assemblies are the compiled code. They are created as an output of a program. The CLR (Common Language Runtime) uses these assemblies and convert into machine language during the execution of that program. This article is an introduction to .NET assemblies and explain how to create .Net assemblies? Summary of the article:

What is a .Net Assembly?

Properties of Assemblies

Types of Assembly

Difference Between Namespace and Assembly

What is a .Net Assembly?
Assembly is the fundamental part of .Net Framework based applications. It is a collection of types and resource information that are built to work together and form a logical unit of functionality. It is the smallest unit of a .Net application for deployment, version control, security permissions, recycle, and activation scoping. It can be an executable (.exe) file or a dynamic link library (DLL) file. The executable file can be run directly from the Windows without any other programs. The library files are used by the others applications.

Assemblies are compiled codes. After the compilation of any .Net Framework based applications all the source codes are converted into an intermediate language. This is called Microsoft Intermediate Language (MSIL). This MSIL is the assembly. The CLR of .Net Framework then convert it into machine language. Each project has only one assembly file. Every time when we compile the project then this assembly file is updated.

When we compile our project, the compiler generates two files inside the Bin folder of the project according to project name. One is “ProjectName.dll/ProjectName.exe” and another one is “ProjectName.pdb”. This “ProjectName.dll/ProjectName.exe” file is called assembly. The file ProjectName.pdb (pdb= Program Database) contains debugging information’s.

Assemblies can be either static or dynamic. Static Assemblies are stored on the disk permanently. They can include .NET Framework classes, interfaces as well as resource files (bitmaps, JPEG files). They are not loaded directly from the memory. They are loaded form the disk when CLR request them. When we compile the C# codes Static Assemblies are generated. They are stored on disk in portable executable (PE) files.

Dynamic Assemblies are not stored on the disk before execution. They are not saved to disk before execution. We can save them to disk after they have executed. They are loaded form the memory directly. They are created dynamically at run time when application request for them.

We can create assemblies in different ways. We can use different development tools, such as Visual Studio, Windows Software Development Kit (SDK). We can also use common language run time APIs, such as “Reflection.Emit” for dynamic assemblies. To use an assembly in a application, we must need to include the reference of that assembly.

Properties of Assemblies
Microsoft .Net Assemblies has the following properties:

Assemblies are implemented as .exe or .dll files

Assemblies are only loaded into memory if they are required. If they are not used or required, they are not loaded

We can share an assembly between applications by putting it in the global assembly cache. For that assemblies must be strong-named before they can be included in the global assembly cache

By using reflection we can programmatically obtain information about an assembly. Only for inspection it a reflection method “ReflectionOnlyLoadFrom” can be used

Types of Assembly
Assemblies are mainly two types: Private Assembly and Public Assembly. There is also a third and least known type of an assembly: Satellite Assembly. A short descriptions about different type of assemblies are given bellow:

Private Assembly
Generally a private assembly is stored in the application’s directory and used by a single application.

Public/Shared Assembly
A share assembly is stored in the Global Assembly Cache (GAC) which is a repository of assemblies maintained by the .Net Framework. It can be used by multiple applications at a time. Every computer where the common language runtime (CLR) is installed has a machine-wide code cache called the global assembly cache (GAC). The global assembly cache stores assemblies specifically selected to be shared by several applications on the computer. In.NET Framework 4, the default location of the global assembly cache is “%windir%\Microsoft.NET\assembly”. In two ways we can deploy an assembly into the global assembly cache:

By using an installer which is designed to work with the global assembly cache. This is more preferable

By using developer tool Global Assembly Cache tool (Gacutil.exe). It is provided by the Windows Software Development Kit (SDK)

Satellite Assembly
A Satellite Assembly includes only static objects (images and other non-executable files) required by the application.It contains resources specific to a given language. In satellite assemblies, we can place resources for different languages in different assemblies. The appropriate assembly is loaded into memory only if the user selects to view the application in that language. The satellite assembly is generally used to create multilingual based applications.

Difference Between Namespace and Assembly
In Microsoft .Net, A Namespace provides the fundamental unit of logical code grouping. It is a logical group of related classes that can be used by others. It is mainly used to organize the classes logically. It provides a way of grouping type names that reduce the name duplicity.
A .Net Assembly provides a fundamental unit of physical code grouping.

The .Net Framework allows us to group our codes in several ways. The assemblies help us to groups our codes physically so that we can establish security, versioning, deployment boundaries. The namespace help us to group our classes logically. These two methods of organization complement each other, and need to use both of them in our .Net development.

About Author

Rashed

I’m Rashedul Alam, software engineer/architect, technical blogger, and educator. I have over 10 years of working experience in the field of software design and development. I like to share my knowledge and technical stuff with others. I am a community contributor, providing problem solving answer through different forum.