Concept of Shared Assembly in .NET

Introduction:

Assembly

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 collection of information to execute an application.

Types of Assembly

  1. Private  assembly (.dll and .exe  are  at 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 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 same folder & main file can't direct 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 shared assembly.

Steps to create Shared assembly

Step 1: Create a class file
 
using System;
namespace SharedAssembly
{
    public class Bike
    {
        public void start()
        {
            Console.WriteLine("kick start ");
        }
   }
}


Step 2: Generate the token

This token known as strong name key. It is the string that is large collection of alphabet and numeric value, 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 

keygeneration.gif 

This will create a key having name shrd.snk(128 bit key ).

Note: To generate the key it use RSA2 (Rivest Shamir Alderman) algorithm.

 
Step 3: Apply the key on our class file by written the code in .dll source file

 
// add this code to your class file
using System.Reflection;
[assembly:AssemblyKeyFile("shrd.snk")]

Class file

using System;
using System.Reflection;
[assembly:AssemblyKeyFile("shrd.snk")]
namespace SharedAssembly
{
    public class Bike
    {
        public void start()
        {
            Console.WriteLine("kick start ");
        }
   }
}

Step 4: Complied the code file & Create a .dll file of Bike class

dll-creation.gif

Step 5: Now register/install .dll into GAC. GAC is the database of CLR in case of .NET.After installing this .dll in GAC, any file can use it with the help of CLR.

To install

D:\shared assembly>gacutil /i sharedAss.dll

gac-utill.gif 

Step 6: The Client Application are as follow 

using System;
using SharedAssembly;
public class MainP
{
    public static void Main(string []args)
    {
     Bike bk=new Bike();
     bk.start();
     Console.Read();
   }
}

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:

D:\>MainP

Up Next
    Ebook Download
    View all
    Learn
    View all