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 an extension of .exe
  • Library Assemblies: It's having an 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 at the system level, there is no need to copy the assembly into the 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 the 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 Assembly
 
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.         /// <summary>  
  11.         /// return ADDITION result of number1 + number2  
  12.         /// </summary>  
  13.         /// <param name="number1"></param>  
  14.         /// <param name="number2"></param>  
  15.         /// <returns></returns>  
  16.         public int Addition(int number1, int number2)  
  17.       {  
  18.                 int result = number1 + number2;  
  19.                 return result;  
  20.             }  
  21.             /// <summary>  
  22.             /// return SUBTRACT result of number1 - number2  
  23.             /// </summary>  
  24.             /// <param name="number1"></param>  
  25.             /// <param name="number2"></param>  
  26.             /// <returns></returns>  
  27.         public int Subtraction(int number1, int number2)  
  28.       {  
  29.             int result = number1 - number2;  
  30.             return result;  
  31.         }  
  32.   
  33.     }  
  34. }  
Consume PrivateAssembly
 
Create a new web form project in the same SOLUTION to implement a PRIVATEASSEMBLY class library.
 
SOLUTION:
A collection of the 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 the reference of PRIVATEASSEMBLY to the ConsumePrivateAssemblyWebApplicaton project.
 
a. First build your project by right-clicking on the 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 the AddReference option. Both projects under one solution, that's why I have selected the Solution tag and select the PrivateAssembly file.
 
AddReference
Now again Right-click on ConsumePrivateAssemblyWebApplicaton and select the 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 TYPE CONTROL 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 the 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 Assembly
 
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.         /// <summary>  
  11.         /// return ADDITION result of number1 + number2  
  12.         /// </summary>  
  13.         /// <param n  
  14.         /// dfgfgame="number1"></param>  
  15.         /// <param name="number2"></param>  
  16.         /// <returns></returns>  
  17.         public int Addition(int number1, int number2)   
  18.       {  
  19.             int result = number1 + number2;  
  20.             return result;  
  21.         }  
  22.         /// <summary>  
  23.         /// return SUBTRACT result of number1 - number2  
  24.         /// </summary>  
  25.         /// <param name="number1"></param>  
  26.         /// <param name="number2"></param>  
  27.         /// <returns></returns>  
  28.         public int Subtraction(int number1, int number2)  
  29.       {  
  30.             int result = number1 - number2;  
  31.             return result;  
  32.         }  
  33.     }  
  34. }  
2. Now rebuild the CalculatorPublicLibrary project.
3. Now, I am going to convert this library into a 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 a Strong Name Key (SNK) file with the 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 the PROPERTIES option.
 
PROPERTIES
8. Browse the SNK file,
 
file
 
9.
I had selected CalculatorPublicLibrary.Snk File.
 
CalculatorPublicLibrary
 
10.
Now, I am going to build the CalculatorPublicLIbrary project, this will generate a strong-named assembly (DLL).
 
11.
Again start the Command line tool from the VisualStudio program group and run as Administrator.
 
12.
Now, I am going 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 questions on this topic.