Concept of Shared Assembly in .NET

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 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 places).
  3. Dynamic assembly (dynamically created).
  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 the main file are not in the same folder & the main file can't directly access the .dll file. It must take permission from the Runtime manager to use that .dll, and 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

using System;

namespace SharedAssembly
{
    public class Bike
    {
        public void start()
        {
            Console.WriteLine("kick start ");
        }
    }
}

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 a very big-string. It is also so long so that no one can access it without any permission.

open the command prompt of Visual Studio.

D:\shared assembly> sn -k shrd.snk

Assembly

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

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

Step 3. Apply the key on our class file by writing the code in the .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 with the code file & Created a .dll file of the Bike class.

Code file

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 be used it with the help of CLR.

To install

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

Install

Step 6. The Client Application is as follows.

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


Recommended Free Ebook
Similar Articles