Pages






Monday, February 28, 2011

Private and Share Assembly

In this article, i have tried to explain the difference between Private and Shared Assembly. I have also tried to give the steps to create such assemblies.

Introduction


An assembly is a compiled code library in .NET Framework. It is a logical unit of code.


Types of Assemblies


Assemblies are of two types.
  1. Private Assemblies
  2. Shared Assemblies

Private Assemblies


Assemblies which are used by single application are called "Private Assemblies". In this case, the bin\Debug\*.dll gets copied in the folder, in which the client application is present.
Let me cite an example here.

Start->All Programs->MS VS 2008->MS VS 2008->File->New->Project->ClassLibrary





I have named it as HappyNewYear. The code follows here:




Now it is the time to compile. So click at Build->Build Solution.
With this, HappyNewYear.dll is created at C:\HappyNewYear\HappyNewYear\bin\Debug




We have to use our .dll file in new ConsoleApplication. So create a new ConsoleApplication. It is named as UseOfDll.




Right Click on Solution Explorer->Add Reference->

With this Add Reference Dialog box open. From here, we have to browse our respective .dll file and press OK button.




Now, Solution Explorer shows, that .dll file is being added under References. We have used the HappyNewYear namespace.




Let me run the program and get the output.




Here using Private Assembly, .dll gets copied in the client application folder.



Shared Assemblies


Shared Assemblies are kept in Global Assembly Cache. Client application using Shared Assemblies need not have its own copy of dll. Shared Assemblies are present in C:\WINDOWS\assembly folder.

Steps to create Shared Assembly:

1. Create a .dll file. (Here i am using my existing HappyNewYear.dll)
2. Now let me create unique assembly name using SN utility(Shared Name utility).
The syntax for it is: sn -k filename.snk

Here sn stands for strong name, -k stands for key and key is stored in filename.snk .



Key pair is created now. The following figure shows the strong name key pair is created.





3. Sign the .dll with the private key by modifying AssemblyInfo file.

AssemblyInfo.cs file is present under Properties in the Solution Explorer. Here i have to give the path of the HappyNewYear.snk file.

[assembly: AssemblyKeyFile("C:\\HappyNewYear\\HappyNewYear\\HappyNewYear.snk")]






4. Compile the dll again to get the assembly signed.

5. And at last, place the dll in Global Assembly Cache(GAC). For that we have a gacutil.exe tool. The syntax is:
gacutil /i assemblypath

/i option installs an assembly into the Global Assembly Cache.



Now let's have a look on our Shared Assembly named "HappyNewYear" present in C:\Windows\Assembly folder.



Let me use this Shared Assembly in one application.




Here no copy of dll is created within the Debug folder of the application.








SOURCE:http://www.dotnetfunda.com/articles/article1117-private-and-share-assembly-.aspx

0 comments: