Private Public Assembly in .NET

In this article you will learn about Private Public Assembly in .NET.

Assembly in .NET

Assembly is a compiled code and logical unit of code. There are two types of assemblies available in .Net.
  • Process Assemblies: I'ts having extension of .exe
  • Library Assemblies: It's having extension of .dll.
Library Assembly is further divided into the following types:
  • Private Assembly
  • Public or Shared Assembly
  • Satellite Assembly
Private Assembly:

Private assembly requires us to copy separately in all application folders where we want to use that assembly’s functionalities; without copying we cannot access the private assembly features and power. Private assembly means every time we have one, we exclusively copy into the BIN folder of each application folder.
 
Public Assembly:

Public assembly is not required to copy separately into all application folders. Public assembly is also called Shared Assembly. Only one copy is required in system level, there is no need to copy assembly into application folder.
Public assembly should install in GAC.

GAC (Global Assembly Cache):
 

When the assembly is required for more than one project or application, we need to make the assembly with a strong name and keep it in GAC or in Assembly folder by installing the assembly with the GACUtil command.

Satellite Assembly:

Satellite assemblies are used for deploying language and culture-specific resources for an application.

Step by Step making of Private Assenbly

Create a class library project.
File - Project - Class Library
Give name: PrivateAssembly

library

Rename CLASS1.CS file TO PrivateAssembly.cs.
CODE of PRIVATEASSEMBLY.CS
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace PrivateAssembly  
  7. {  
  8.     public class PrivateAssembly  
  9.     {  
  10.   
  11.   
  12.         /// <summary>  
  13.         /// return ADDITION result of number1 + number2  
  14.         /// </summary>  
  15.         /// <param name="number1"></param>  
  16.         /// <param name="number2"></param>  
  17.         /// <returns></returns>  
  18.         public int Addition(int number1, int number2)  
  19.       {  
  20.                 int result = number1 + number2;  
  21.                 return result;  
  22.             }  
  23.             /// <summary>  
  24.             /// return SUBTRACT result of number1 - number2  
  25.             /// </summary>  
  26.             /// <param name="number1"></param>  
  27.             /// <param name="number2"></param>  
  28.             /// <returns></returns>  
  29.         public int Subtraction(int number1, int number2)  
  30.       {  
  31.             int result = number1 - number2;  
  32.             return result;  
  33.         }  
  34.   
  35.     }  
  36. }  
Consume PrivateAssembly

Create a new web form project in same SOLUTION to implement a PRIVATEASSEMBLY class library.

SOLUTION:
A collection of project(s) called solutions. Every project is stored inside a solution.

project

Right click on SOLUTION
File - New - Project - Asp.Net Web Form Application.

Form

Give reference of PRIVATEASSEMBLY to ConsumePrivateAssemblyWebApplicaton project.

a. First build your project by right clicking on PrivateAssembly project and select BUILD.
b. Right click on ConsumePrivateAssemblyWebApplicaton and select option Open Folder in File Explorer.

Explorer

In the above image you can see there is no privateassembly.dll. 

c. Right click on ConsumePrivateAssemblyWebApplicaton and select AddReference option. Both project under one solution, that's why I have selected Solution tag and select PrivateAssembly file.
AddReference

Now again Right click on ConsumePrivateAssemblyWebApplicaton and select option Open Folder in File Explorer and check PrivateAssembly file comes in this project after the given reference.

reference

Right click on WebForm Project and Add New Web Form,

WebForm
WebForm

I had created WebForm1.aspx file. Now, double click on WebForm1.aspx

Drag and Drop Following controls from TOOLBOX.

CONTROL TYPECONTROL ID DESCRIPTION
 HTML Table Drag n Drop HTML table from toolbox total 9 rows required
 TextBox  txtNumber1  To receive Number1
 TextBox  txtNumber2  To receive Number2
 Button  btnAddition  To do Addition function
 Button  btnSubtract  To do subtract function
 Label  lblResult  To display Result function.

Give reference in WebForm1.aspx.cs (code behind file)

using PrivateAssembly;

Full Code of WebForm1.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ConsumePrivateAssenblyWebApplicaton.WebForm1" %>  
  2.   
  3.     <!DOCTYPE html>  
  4.   
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.         <style type="text/css">  
  10.             .auto-style1 {}  
  11.               
  12.             .auto-style2 {  
  13.                 width: 94px;  
  14.             }  
  15.         </style>  
  16.     </head>  
  17.   
  18.     <body>  
  19.         <form id="form1" runat="server">  
  20.             <div>  
  21.                 <table style="width:100%;">  
  22.                     <tr>  
  23.                         <td class="auto-style2">Number1</td>  
  24.                         <td>  
  25.                             <asp:TextBox ID="txtNumber1" runat="server"></asp:TextBox>  
  26.                         </td>  
  27.                         <td> </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td class="auto-style2">Number2</td>  
  31.                         <td>  
  32.                             <asp:TextBox ID="txtNumber2" runat="server"></asp:TextBox>  
  33.                         </td>  
  34.                         <td> </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td class="auto-style2"> </td>  
  38.                         <td> </td>  
  39.                         <td> </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td class="auto-style1" colspan="2">  
  43.                             <asp:Button ID="btnAddition" runat="server" OnClick="btnAddition_Click" Text="Addition" />  
  44.                             <br />  
  45.                             <asp:Button ID="btnSubtract" runat="server" OnClick="btnSubtract_Click" Text="Subtract/Minus" />  
  46.                         </td>  
  47.                         <td> </td>  
  48.                     </tr>  
  49.                     <tr>  
  50.                         <td class="auto-style2"> </td>  
  51.                         <td> </td>  
  52.                         <td> </td>  
  53.                     </tr>  
  54.                     <tr>  
  55.                         <td class="auto-style2">Result</td>  
  56.                         <td>  
  57.                             <asp:Label ID="lblResult" runat="server" Text="[Result]"></asp:Label>  
  58.                         </td>  
  59.                         <td> </td>  
  60.                     </tr>  
  61.                     <tr>  
  62.                         <td class="auto-style2"> </td>  
  63.                         <td> </td>  
  64.                         <td> </td>  
  65.                     </tr>  
  66.                     <tr>  
  67.                         <td class="auto-style2"> </td>  
  68.                         <td> </td>  
  69.                         <td> </td>  
  70.                     </tr>  
  71.                 </table>  
  72.             </div>  
  73.         </form>  
  74.     </body>  
  75.   
  76.     </html>  
Full Code of WebForm1.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using PrivateAssembly;  
  8.   
  9. namespace ConsumePrivateAssenblyWebApplicaton   
  10. {  
  11.     public partial class WebForm1: System.Web.UI.Page   
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.   
  16.         }  
  17.   
  18.         protected void btnAddition_Click(object sender, EventArgs e)   
  19.         {  
  20.             PrivateAssembly.PrivateAssembly _pvt = new PrivateAssembly.PrivateAssembly();  
  21.             lblResult.Text = Convert.ToString(_pvt.Addition(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));  
  22.         }  
  23.   
  24.         protected void btnSubtract_Click(object sender, EventArgs e)   
  25.         {  
  26.             PrivateAssembly.PrivateAssembly _pvt = new PrivateAssembly.PrivateAssembly();  
  27.             lblResult.Text = Convert.ToString(_pvt.Subtraction(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));  
  28.         }  
  29.     }  
  30. }  
Step by Step making of Private Assenbly

Add a class library project.

Right click on Solution and Add - New Project - Class Library
Give name: CalculatorPublicLibrary
CalculatorPublicLibrary

1.
Rename default file name CLASS1.CS into PubCalculator.cs

Copy the PRIVATEASSEMBLY.CS code as its.
Code of PUBCALCULATOR.CS
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CalculatorPublicLibrary  
  7. {  
  8.     public class PubCalculator  
  9.     {  
  10.   
  11.   
  12.         /// <summary>  
  13.         /// return ADDITION result of number1 + number2  
  14.         /// </summary>  
  15.         /// <param n  
  16.         /// dfgfgame="number1"></param>  
  17.         /// <param name="number2"></param>  
  18.         /// <returns></returns>  
  19.         public int Addition(int number1, int number2)   
  20.       {  
  21.             int result = number1 + number2;  
  22.             return result;  
  23.         }  
  24.   
  25.   
  26.         /// <summary>  
  27.         /// return SUBTRACT result of number1 - number2  
  28.         /// </summary>  
  29.         /// <param name="number1"></param>  
  30.         /// <param name="number2"></param>  
  31.         /// <returns></returns>  
  32.         public int Subtraction(int number1, int number2)  
  33.       {  
  34.             int result = number1 - number2;  
  35.             return result;  
  36.         }  
  37.     }  
  38. }  
2. Now rebuild CalculatorPublicLibrary project.
3. Now, I am going to convert this library as public library.

4. Click on VisualStudio program group and Visual Studio Tools  Developer Command Prompt for VS2012 (RUN AS ADMINISTRATOR).

VisualStudio

5.
First I have to create Strong Name Key (SNK) file with following command:
Syntax: SN –K <FileNameToCreate>
Example: SN –K CalculatorPublicLibrary.snk
This will create CALCULATORPUBLICLIBRARY.SNK file.
CALCULATORPUBLICLIBRARY

6.
Check in Windows explorer, E:\Windows\System32.

Windows

7.
Select CalculatorPublicLibrary project and Right click. Select PROPERTIES option.

PROPERTIES
8. Browse the SNK file,

file

9.
I had selected CalculatorPublicLibrary.Snk File.

CalculatorPublicLibrary

10.
Now, I am going to build CalculatorPublicLIbrary project, this will generate strong named assembly (DLL).

11.
Again start Command line tool from VisualStudio program group and run as Administrator.

12.
Now, I am oing to coping the DLL into GAC (Global Assembly Cache).
Switch to CalculatorPublicLIbrary project in command prompt and change directory to BIN folder.

13.
Syntax: gacutil –I <File Name>

[To install dll into GAC]
gacutil –U <File Name>
[To UnInstall dll from GAC]

Example:
gacutil –I CalculatorPublicLibrary.dll
(to copy dll into GAC.)

Syntax

14.
GAC location.
  • .NET 1.0 - NET 3.5: c:\windows\assembly (%systemroot%\assembly)
  • .NET 4.x: %windir%\Microsoft.NET\assembly
15. Now , I have to give the reference to ConsumePrivateAssemblyWebApplication

WebApplicaton project.

Right click on ConsumePrivateAssemblyWebApplication and Select ADD  Add Reference to CalculatorPublicLibrary this is public library.

library

16.
Build the ConsumePrivateAssemblyWebApplication project Check BIN folder to confirm public library CalculatorPublicLibrary.dll should not be copied into this folder, but We can access the same.

17.
Now add new WebForm called WebForm2
18. code of WebForm2.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="ConsumePrivateAssenblyWebApplicaton.WebForm2" %>  
  2.     <!DOCTYPE html>  
  3.   
  4.     <html xmlns="http://www.w3.org/1999/xhtml">  
  5.   
  6.     <head id="Head1" runat="server">  
  7.         <title></title>  
  8.         <style type="text/css">  
  9.             .auto-style1 {}  
  10.               
  11.             .auto-style2   
  12.             {  
  13.                 width: 94px;  
  14.             }  
  15.         </style>  
  16.     </head>  
  17.   
  18.     <body>  
  19.         <form id="form1" runat="server">  
  20.             <div>  
  21.                 <table style="width:100%;">  
  22.                     <tr>  
  23.                         <td class="auto-style2">Number1</td>  
  24.                         <td>  
  25.                             <asp:TextBox ID="txtNumber1" runat="server"></asp:TextBox>  
  26.                         </td>  
  27.                         <td> </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td class="auto-style2">Number2</td>  
  31.                         <td>  
  32.                             <asp:TextBox ID="txtNumber2" runat="server"></asp:TextBox>  
  33.                         </td>  
  34.                         <td> </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td class="auto-style2"> </td>  
  38.                         <td> </td>  
  39.                         <td> </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td class="auto-style1" colspan="2">  
  43.                             <asp:Button ID="btnAddition" runat="server" OnClick="btnAddition_Click" Text="Addition" />  
  44.                             <br />  
  45.                             <asp:Button ID="btnSubtract" runat="server" OnClick="btnSubtract_Click" Text="Subtract/Minus" />  
  46.                         </td>  
  47.                         <td> </td>  
  48.                     </tr>  
  49.                     <tr>  
  50.                         <td class="auto-style2"> </td>  
  51.                         <td> </td>  
  52.                         <td> </td>  
  53.                     </tr>  
  54.                     <tr>  
  55.                         <td class="auto-style2">Result</td>  
  56.                         <td>  
  57.                             <asp:Label ID="lblResult" runat="server" Text="[Result]"></asp:Label>  
  58.                         </td>  
  59.                         <td> </td>  
  60.                     </tr>  
  61.                     <tr>  
  62.                         <td class="auto-style2"> </td>  
  63.                         <td> </td>  
  64.                         <td> </td>  
  65.                     </tr>  
  66.                     <tr>  
  67.                         <td class="auto-style2"> </td>  
  68.                         <td> </td>  
  69.                         <td> </td>  
  70.                     </tr>  
  71.                 </table>  
  72.             </div>  
  73.         </form>  
  74.     </body>  
  75.   
  76.     </html>  
19. code of WebForm2.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using CalculatorPublicLibrary;  
  8.   
  9. namespace ConsumePrivateAssenblyWebApplicaton  
  10. {  
  11.     public partial class WebForm2: System.Web.UI.Page   
  12.     {  
  13.   
  14.         protected void Page_Load(object sender, EventArgs e)   
  15.         {  
  16.   
  17.         }  
  18.   
  19.         protected void btnAddition_Click(object sender, EventArgs e)   
  20.         {  
  21.             CalculatorPublicLibrary.PubCalculator _pvt = new CalculatorPublicLibrary.PubCalculator();  
  22.             lblResult.Text = Convert.ToString(_pvt.Addition(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));  
  23.         }  
  24.   
  25.         protected void btnSubtract_Click(object sender, EventArgs e)   
  26.         {  
  27.             CalculatorPublicLibrary.PubCalculator _pvt = new CalculatorPublicLibrary.PubCalculator();  
  28.             lblResult.Text = Convert.ToString(_pvt.Subtraction(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));  
  29.         }  
  30.     }  
  31. }  
20. Checked there is no public library.

library

Please, feel free to ask any question on this topic.