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
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 same SOLUTION to implement a PRIVATEASSEMBLY class library.
SOLUTION: A collection of project(s) called solutions. Every project is stored inside a solution.
Right click on SOLUTION
File - New - Project - Asp.Net Web Form Application.
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.
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.
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.
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 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
- <%@ 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 Assenbly
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 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).
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.
6. Check in Windows explorer, E:\Windows\System32.
7. Select CalculatorPublicLibrary project and Right click. Select PROPERTIES option.
8. Browse the SNK file,
9. I had selected CalculatorPublicLibrary.Snk File.
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.)
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.
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 question on this topic.