Saturday, January 24, 2009

How to make an assembly as shared or global assembly.

Private assemblies can be made as shared assembly by placing them in shared assembly folder (c:\windows\assembly).

Please refer Private and Global assembles in Prog.Findings for difference between global and private assemblies.

But it is not simple step to just copy and paste in assembly folder.
There are certain steps that are to be followed for doing so.

Step 1: Prepare private assembly which you are willing to make it as global or shared.
I am creating a class library named “MsgBox”, in which I have created a class file named “Msg.cs”.
This functionality is to display a message box with the text sent as parameter.
So once if this dll is shared globally among applications, the same function can be used to display message box at all instants.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MsgBox
public class Msg
public void DisplayMessage(string argMessage)

After building the project solution, I can find a dll file created inside the bin folder which is said to be as the private assembly.
I wish to make this as global assembly and use the same dll file in other application when and there required.

Step 2: This dll has to be strongly named before placing this into the global assembly folder (c:\windows\assembly).
Strongly naming is a necessary step, else if we try to drag and drop the private assembly into c:\windows\assembly folder with out strongly naming it,
It says “Failure adding assembly to the cache: Attempt to install an assembly without a strong name”.

To strongly name the private assembly:
Step i) Open .NET command prompt and set the prompt path to the private assembly’s folder path.
sn –k [filename as your wish].snk
Example: - sn –k mykey.snk

Step ii) You can identify the key file generated in the private assembly folder and with the name given (mykey.snk).

Step iii)
Open the “AssemblyInfo.cs” file under properties folder in the class library.
Mention the path of the generated key file as
[assembly: AssemblyKeyFile([key file path])]
In my example

[assembly: AssemblyKeyFile("C:/Users/srinivas/My personals/Testing/MsgBox/MsgBox/bin/Release/mykey.snk")]

Now build the solution, the dll file which has been created after building the solution is the assembly which is strongly named.
So now this assembly can be globally shared.
Step 3: Now this strongly named private assembly can be globally shared by either dragging and dropping the dll into assembly folder (c:\windows\assembly) or
By registering the private assembly using .NET Command prompt.
I am using .NET command prompt to do so.
Gacutil –I [dll file path]

Now you can find this private assembly as global assembly in “c:\windows\assembly folder.

Step 5: we should copy and paste this private assembly inside frame work folder, such that we can view this dll that can be added through “Add Reference” dialog in .NET solutions.
Framework folder is at
C:\Windows\Microsoft.NET\Framework\v2.0.50727, inside which we can find many dll files that we use while building .NET applications.
Simillarly we shall copy and paste our private assembly into this folder.

Step 6:Now I would like to build a new .NET application in a separate solution and in a separate folder, entirely
I am creating a “Demo1” application, in which I have created a windows form saying “MyApp”.

When I click on this add references, I can find that private assembly which has been globally shared in “c:\windows\assembly folder.

I am using this MsgBox.dll file whose functionality is to display a message box.
Windows form :


No comments:

Post a Comment