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 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
Rename CLASS1.CS file TO PrivateAssembly.cs.
CODE of PRIVATEASSEMBLY.CS
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace PrivateAssembly
- {
- public class PrivateAssembly
- {
-
-
-
-
-
-
- public int Addition(int number1, int number2)
- {
- int result = number1 + number2;
- return result;
- }
-
-
-
-
-
-
- public int Subtraction(int number1, int number2)
- {
- int result = number1 - number2;
- return result;
- }
-
- }
- }
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.
File - New - Project - Asp.Net Web Form Application.
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.
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.
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.
Right-click on WebForm Project and Add New Web Form,
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)
Full Code of WebForm1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ConsumePrivateAssenblyWebApplicaton.WebForm1" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- <style type="text/css">
- .auto-style1 {}
-
- .auto-style2 {
- width: 94px;
- }
- </style>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table style="width:100%;">
- <tr>
- <td class="auto-style2">Number1</td>
- <td>
- <asp:TextBox ID="txtNumber1" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2">Number2</td>
- <td>
- <asp:TextBox ID="txtNumber2" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style1" colspan="2">
- <asp:Button ID="btnAddition" runat="server" OnClick="btnAddition_Click" Text="Addition" />
- <br />
- <asp:Button ID="btnSubtract" runat="server" OnClick="btnSubtract_Click" Text="Subtract/Minus" />
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2">Result</td>
- <td>
- <asp:Label ID="lblResult" runat="server" Text="[Result]"></asp:Label>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
Full Code of WebForm1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using PrivateAssembly;
-
- namespace ConsumePrivateAssenblyWebApplicaton
- {
- public partial class WebForm1: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnAddition_Click(object sender, EventArgs e)
- {
- PrivateAssembly.PrivateAssembly _pvt = new PrivateAssembly.PrivateAssembly();
- lblResult.Text = Convert.ToString(_pvt.Addition(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));
- }
-
- protected void btnSubtract_Click(object sender, EventArgs e)
- {
- PrivateAssembly.PrivateAssembly _pvt = new PrivateAssembly.PrivateAssembly();
- lblResult.Text = Convert.ToString(_pvt.Subtraction(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));
- }
- }
- }
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
1.
Rename default file name CLASS1.CS into PubCalculator.cs
Copy the PRIVATEASSEMBLY.CS code as its.
Code of PUBCALCULATOR.CS
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace CalculatorPublicLibrary
- {
- public class PubCalculator
- {
-
-
-
-
-
-
-
- public int Addition(int number1, int number2)
- {
- int result = number1 + number2;
- return result;
- }
-
-
-
-
-
-
- public int Subtraction(int number1, int number2)
- {
- int result = number1 - number2;
- return result;
- }
- }
- }
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).
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.
6. Check-in Windows explorer, E:\Windows\System32.
7.
Select CalculatorPublicLibrary project and Right-click. Select the PROPERTIES option.
8. Browse the SNK file,
9.
I had selected CalculatorPublicLibrary.Snk File.
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.)
- .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
Right-click on ConsumePrivateAssemblyWebApplication and Select ADD Add Reference to CalculatorPublicLibrary this is public 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
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="ConsumePrivateAssenblyWebApplicaton.WebForm2" %>
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head id="Head1" runat="server">
- <title></title>
- <style type="text/css">
- .auto-style1 {}
-
- .auto-style2
- {
- width: 94px;
- }
- </style>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table style="width:100%;">
- <tr>
- <td class="auto-style2">Number1</td>
- <td>
- <asp:TextBox ID="txtNumber1" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2">Number2</td>
- <td>
- <asp:TextBox ID="txtNumber2" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style1" colspan="2">
- <asp:Button ID="btnAddition" runat="server" OnClick="btnAddition_Click" Text="Addition" />
- <br />
- <asp:Button ID="btnSubtract" runat="server" OnClick="btnSubtract_Click" Text="Subtract/Minus" />
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2">Result</td>
- <td>
- <asp:Label ID="lblResult" runat="server" Text="[Result]"></asp:Label>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td> </td>
- <td> </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
19. code of WebForm2.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using CalculatorPublicLibrary;
-
- namespace ConsumePrivateAssenblyWebApplicaton
- {
- public partial class WebForm2: System.Web.UI.Page
- {
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnAddition_Click(object sender, EventArgs e)
- {
- CalculatorPublicLibrary.PubCalculator _pvt = new CalculatorPublicLibrary.PubCalculator();
- lblResult.Text = Convert.ToString(_pvt.Addition(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));
- }
-
- protected void btnSubtract_Click(object sender, EventArgs e)
- {
- CalculatorPublicLibrary.PubCalculator _pvt = new CalculatorPublicLibrary.PubCalculator();
- lblResult.Text = Convert.ToString(_pvt.Subtraction(Convert.ToInt16(txtNumber1.Text), Convert.ToInt16(txtNumber2.Text)));
- }
- }
- }
20. Checked there is no public library.
Please, feel free to ask any questions on this topic.