Private and Share Assembly

Ganeshji
Posted by in .NET Framework category on for Beginner level | Points: 250 | Views : 59242 red flag
Rating: 4 out of 5  
 2 vote(s)

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.






Happy Reading!


Reference


  • http ://www. dotnetspider.com/forum/43223-how-create-Shared-Assembly.aspx
  • http ://www. dnzone.com/go?698
  • http :// sharpertutorials.com/creating-strong-named-assemblies/
  • http ://www. c-sharpcorner.com/UploadFile/hemantpatil/111222007032935AM/1.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Ganeshji
Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji


Login to vote for this post.

Comments or Responses

Posted by: Karthikanbarasan on: 1/9/2011 | Points: 25
Nice article for the beginners...
Posted by: Ganeshji on: 1/10/2011 | Points: 25
Thanx! Keep reading and give suggestions.
Posted by: Ddd on: 1/14/2011 | Points: 25
Dear Sir,

Assemblies can also be classified as:

1)Private and Shared assemblies.

2)Static and Dynamic assemblies.

3)SingleFile and MultiFile assemblies.

Posted by: Ganeshji on: 1/14/2011 | Points: 25
Thnx! In future, i shall try to cover rest of the assemblies in my article. Plz keep suggesting me. Thnx again.
Posted by: Akiii on: 5/15/2011 | Points: 25
nice article...

Regards
Akiii
Posted by: Ganeshji on: 5/15/2011 | Points: 25
Thnx AKiii!

Login to post response

Comment using Facebook(Author doesn't get notification)