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.
After compilation of each project, an .exe or .dll file is generated, known as Assembly. The name of an assembly file is the same as the project name. The assembly of a project present under bin\Debug folder of that project folder. After successful build of each project an assembly is generated inside that folder.We can use assembly files by our requirement we may directly take the assembly file and install into client meachine if it is .exe type or we can consume the file in another project if the assembly is dll type.
Class Library and Windows Forms Control Library projects generate a .dll assembly where as Windows Forms Applications, Console Applications, WPF Applications, and Windows Services projects generate an .exe assembly.
.Exe type assembly are run by their own as well as they provide the support to other in their execution process so these assembly are know as in-process component where as .dll type assembly are called as out process component because they are only developed for the purpose to consume inside .exe type of application.
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:
- Private Assembly
- Shared Assembly
Private Assembly
When we build any project we found a corresponding assembly inside bin/Debug folder; so by default these assembly is private type.It' private because when we add that assembly reference to another project then a copy of that assembly created inside that project.Bellow figure is an example of Private assembly.
Creating an assembly to test if it is private, by default:
Start a new project inside Visual studio of type Class Library and name it as “PAssembly”,we take class librery because we can consume this project dll file inside another project. Here i am using VS2012 for my project creation.
in this project you find a file called as 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” or Press
"Ctrl+shift+B" which will bulid + compile your project and generate an assembly with the name as PAssembly.dll.
Note: We can find the path for our assembly in the output window.
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
As we saw above while consuming private assembly it always created a copy of that file inside the project bin/debug folder;so if we want to use that assembly in several application then we required to put that assembly inside a shared folder. That shared folder in .Net called as Global Assembly Cache folder. We can add number of assembly inside that folder and consume that assembly any project.
Note: Before consuming we need to add the assembly inside GAC folder; due to GAC is present under window directory so read,write and delete can only available when we give the permission to that folder.
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
/l: List
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 ?
When an assembly created by using Name + Version + Public Key then it's called as strong name
- Name: Every assembly has it's own name which comes by default when you build your project.
- Version: All software comes with Version number; without version number we can not identify it, so when we created assembly it's comes with default version number called as "1.0.0.0"
- Public Key: A public key is a key thrugh which GAC can identify the assembly. As we know GAC contain all the assembly so to identify uniquely GAC uses this public key.
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>
E.g.: sn -k Key.snk
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 1.0.0.0";
- }
To available key file with in the project we need to follow bellow step :-
Step-1 :-Open the project property window.
Step-2 : Click on Signin tab then you see a checkbox for Sign the assembly; you need to checked their and then select your key form the browse option as bellow.
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:
<drive>:\<folder\SAssembly\SAssembly\bin\Debug> gacutil -i SAssembly.dll
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 1.0.0.0”.
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();
- MessageBox.Show(obj.TestHello());
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
As i said in above that every assembly has a version number; so to define the version number we have some protocol through which we can give the version name with their different build. So all this information can found inside AssemblyInfo.cs file inside each project.
The AssemblyInfo.cs it contain following information :-
- Major Version
- Minor Version
- Build Number
- Revision
This is all about .NET Assembly and their uses.
So, I think I have covered all the topics successfully.