Assemblies

 Def: An assembly is the container of compiled code of .NET applications.
 It contains the code in MSIL (Microsoft Intermediate Language) language.
 At run time, it will be compiled by CLR’s JIT compiler into machine language.

Types of Assemblies (based on extension):
1. Executable Assemblies / Process Assemblies (with .EXE extension)
Generated with Console Applications and Windows Applications.
2. Library Assemblies (with .DLL extension)
Generated with Class Library Projects.

Types of Assemblies:
1. Private Assemblies
Limited to access with only one application. All of our previous applications come under private assemblies.

2. Shared Assemblies
These are shared by one or more applications. These are implemented for re-usability.

Overview of “Class Library” Project

 A “class library” project is meant for the development of “Shared Assemblies”.
 The shared assemblies are re-usable in other applications also.
 That means the class library’s code is re-usable in other applications, whenever required.
 Class library project contains only user-defined classes.
 Whenever the class library project is compiled, a library assembly file (with .dll extension) will be generated.
 The class library project can’t be executed directly unlike console applications, windows applications etc.
assemblies

Global Assembly Cache (GAC): All the shared assemblies should be saved in the GAC. GAC offers the following advantages.
 Unique identification of the shared assemblies.
 Avoid the DLL hell (replacement of an assembly related to one application, by another application).
 Faster accessibility.


To view the currently installed assemblies in the GAC, open the following folder.
C:\windows\assembly
Strong Name Keys (SNK): The “strong name key” is a file, that acts as a unique identifier, for the shared assemblies, stored in the GAC.

Implementation Steps of Shared Assemblies

1. Create a Class Library Project.
• Create a new “Class Library” project.
• Write the required code in the project.


2. Create a strong name key.
• Right click on the project in the “Solution Explorer” and choose “Properties”.
• In the project properties, select the check box “Sign the assembly”.
• In the “Choose a strong name key file” drop down, select “<New>” option.
• In the “Create strong name key” dialog box, enter the name of the strong name key file.
• If password security is not required, uncheck the “Protect my key file with a password” checkbox.
• Click on OK.

3. Customize the “Assembly Information” (AssemblyInfo.cs).
• This is optional step.
• To change the additional details of the assembly like displayed name, version, company, copy right, description etc., open “AssemblyInfo.cs” file from the “Properties” folder in the Solution Explorer.

4. Generate the DLL File.
• Build the class library project.
• Then the “.dll” file will be generated in the “bin\debug” folder of your class library project.

5. Write the assembly into GAC (Global Assembly Cache).
• Open the following folder.
C:\Windows\Assembly
• Drag and drop the “.DLL file” from “bin\Debug” folder into the “c:\windows\assembly” folder.
• After dragging, the name of your shared assembly will appear in the existing list.
• Now, the shared assembly is ready. The rest of your work involved with the usage of the shared assembly.

6. Invoke the Shared Assembly.
• Create the executable project (Console application / windows application).
• Click on “Project” – “Add Reference”.
• Click on “Browse” tab.
• Open the class library project’s “bin\Debug” folder.
• Select the “dll file” and click on OK.
• Then the reference of the selected shared assembly will be added to the current project.
• Then you can construct objects for the required class in the class library and perform required activities on that.