Concept of Shared Assembly in .NET


Assembly is the smallest unit of deployment, it is the collection of various things to perform a common task or goal. It is also called the logical entity that is a collection of information to execute an application.
Types of Assembly
  1. Private  assembly (.dll and .exe  are  at the same place)
  2. Shared assembly (.dll and .exe  are at different place)
  3. Dynamic assembly (dynamically create )
  4. Distributed assembly (in different parts)
  5. Satellite assembly (on the network)

Shared Assembly

For using a shared assembly an application must have a key (token) for authorization and authority.
When .dll and main file are not in the same folder & the main file can't directly access the .dll file. It must take permission for Runtime manager for using that .dll, then the collection to run the file can be known as a shared assembly.

Steps to create Shared assembly

Step 1: Create a class file
  1. using System;  
  2. namespace SharedAssembly  
  3. {  
  4.     public class Bike  
  5.     {  
  6.         public void start()  
  7.         {  
  8.             Console.WriteLine("kick start ");  
  9.         }  
  10.    }  
Step 2: Generate the token
This token is known as a strong name key. It is a string that is a large collection of alphabet and numeric values, it is very big-string. It is also so long so that no one can access without any permission.
--> open command prompt of visual studio
D:\shared assembly> sn -k shrd.snk 
This will create a key having the name shrd.snk(128-bit key ).
Note: To generate the key it uses RSA2 (Rivest Shamir Alderman) algorithm.
Step 3: Apply the key on our class file by written the code in .dll source file
  1.  // add this code to your class file  
  2. using System.Reflection;  
  3. [assembly:AssemblyKeyFile("shrd.snk")]  
  5. Class file  
  7. using System;  
  8. using System.Reflection;  
  9. [assembly:AssemblyKeyFile("shrd.snk")]  
  10. namespace SharedAssembly  
  11. {  
  12.     public class Bike  
  13.     {  
  14.         public void start()  
  15.         {  
  16.             Console.WriteLine("kick start ");  
  17.         }  
  18.    }  
Step 4: Complied the code file & Create a .dll file of Bike class
Step 5: Now register/install .dll into GAC. GAC is the database of CLR in the case of .NET. After installing this .dll in GAC, any file can use it with the help of CLR.
Step 6: The Client Application is as follow 
  1. using System;  
  2. using SharedAssembly;  
  3. public class MainP  
  4. {  
  5.     public static void Main(string []args)  
  6.     {  
  7.      Bike bk=new Bike();  
  8.      bk.start();  
  9.      Console.Read();  
  10.    }  
Step 7: Compiled the whole application
D:\> csc /r:d:\shared assembly\sharedAss.dll MainP.cs
Step 8: Run your program by using the command given below: