In this article, I am going to discuss Strong and Weak Assemblies in .NET Framework with examples. Please read our previous article where we discussed App Domain in .NET Application. In .NET Framework, the assemblies are broadly classified into 2 types. They are as follows:
- Weak Named Assemblies
- Strong Named Assemblies
Let us first understand what assembly is then we will discuss strong and week assembly and the difference between them.
Understanding Assembly in .NET Framework:
Let us create a simple console application with the name AssemblyDemo and then modify the Program class as shown below. This is a very simple C# program, simply printing a message “Hello world” to the console. In order to print the message on the Console, here we are using the Console class. That Console class is coming from the System namespace. And the System namespace is present in the System Assembly. The System assembly is a .NET Framework assembly
- using System;
- namespace AssemblyDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello world");
- Console.ReadKey();
- }
- }
- }
When we installed .NET in the machine, two important components get installed. One is the .NET Framework Class Library (BCL) and the other is CLR which is nothing but the runtime environment. In .NET Framework Class Library, we have several assemblies. All the .NET Framework assemblies are installed in a special location called GAC (Global Assembly Cache). The location for GAC is “C:\Windows\assembly”. Once you go to this location, you will find all the .NET Framework assemblies as shown in the below image. We will discuss GAC in detail in our upcoming article.

All the assemblies present in GAC are strongly typed. Later part of this article we will discuss what exactly a strongly type assembly and difference between a week and a strong type assembly in .NET.
In .NET, an assembly is consists of 4 Parts
- Simple textual name (i.e. the Assembly name).
- The Version number.
- Culture information (If provided, otherwise the assembly is language-neutral)
- Public key token
Let us discuss each part of an assembly in detail.
Assembly Name (Simple Textual Name):
This is nothing but the project name. We just created one console application with the name AssemblyDemo. Now build the project and go to the Bin => Debug folder which you can find inside the project and you should find an assembly with the name AssemblyDemo.
Version Number:
The default format of Version number is 1.0.0.0. That means the version number again consists of four parts as follows:
- Major Version
- Minor Version
- Build Number
- Revision Number
Typically, any software we develop will go under a change over a period of time. When we fix bugs or add new features, depending on the significance of the change, we either change the major number of the minor number. If the changes we are making to the application is huge, then probably we change the major number else we will change the minor number. Most of the time the build number and revision number have defaulted.
In order to see the Version Number of your assembly “AssemblyDemo”, Open Visual Studio Developer command prompt and use ILDASM command to see the version number as shown below.

Once you use the ILDASM command followed by the physical path of your assembly and hit the enter key you will get the following ILDASM window and just look at the version number which you can find at the bottom of the window.

How to change the Version Number of an Assembly in .NET?
If you want to change the version number of your assembly, then you need to use the AssemblyVersion attribute within the AssemblyInfo class which is present inside the Properties folder of your project. You can specify all the values or you can default the Revision and Build Numbers by using the ‘*’. Suppose, you want to change the major number to 3 and the minor number to 2, then you need to change the AssemblyVersion attribute as shown below in the AssemblyInfo class.
[assembly: AssemblyVersion(“3.2.*”)]
With the above changes in place, now if you build the solution and check the version number using the ILDASM tool, then you should get the updated version number. Please read our ILDASM and ILASM article to learn more about ILDASM and ILASM.
Assembly Culture:
The AssemblyCulture attribute is used for specifying the culture of the assembly. By default in .NET assemblies are language-neutral which means the AssemblyCulture attribute contains an empty string. If you go to the GAC, then you will find most of the assemblies are culture neutral. But there could be some assemblies which are culture-specific. For better understand, please have a look at the following image which you can also find in your GAC. The following assemblies are specific to the language specified in the Culture attribute.

When you specify the culture, then that assembly becomes a satellite assembly. We will discuss satellite assemblies in detail in our upcoming article. If you want to specify the culture then you need to use the AssemblyCulture attribute with the AssemblyInfo.cs class file. For example, if you want to specify English as the culture then you need to use the AssemblyCulture attribute as shown below.
[assembly: AssemblyCulture(“en”)]
Public Key Token:
If you go to the GAC, then you will see that every assembly is assigned with a public key token. In order to get the public key token, you need to sign your assembly with a private and public key pair. Now the question is how do I get the private-public key. In the .NET framework, we have a tool called strong naming tool and you can use this tool to generate the key pair. Again in order to use this tool you need to use the Developer Command Prompt for Visual studio. So, open Developer Command Prompt for Visual Studio in administrator mode and then type sn.exe -k c:\MyKeyFile.snk and press enter as shown in the below image.

Once you type the required command and press enter, the key file with the name MyKeyFile.snk should be generated in the C: drive. In SN.exe, SN stands for Strong Name.
Once you generated the Key file, then you need to use the AssemblyKeyFile attribute of the AssemblyInfo class to sign the assembly with a strong name. To the constructor of the AssemblyKeyFile attribute, you need to pass the path of the key file that contains the private and public key as shown below.
[assembly: AssemblyKeyFile(“C:\\MyKeyFile.snk”)]
Once you add the above AssemblyKeyFile attribute, build the solution. Once you build the solution, now your assembly sign with a private-public key pair. Now, our assembly has all the four components such as Name, Version Number, Culture and Public Key Token.
Strong Name Assembly in .NET Framework:
An assembly is said to be strongly named assembly when it has the following properties
- The assembly name.
- Version number.
- The assembly should have been signed with the private/public key pair.
What is the difference between Strong and Weak Assemblies in .NET Framework?
If an assembly is not signed with the private/public key pair then the assembly is said to be a weak named assembly and it is not guaranteed to be unique and may cause the DLL hell. The Strong named assemblies are guaranteed to be unique and solve the DLL hell problem. Again, you cannot install an assembly into GAC unless the assembly is strongly named.
In the next article, I am going to discuss GAC in detail as well as we will discuss how to install a strong name assembly into GAC. Here, in this article, I try to explain what are the Strong and Weak Assemblies in .NET Framework as well as the different properties of an assembly in detail. I hope you enjoy this article.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.