Dot Net Assembly and How to Create Assembly
An assembly is a partially compiled code library for use in deployment, versioning and security. The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET assemblies may or may not be executable.Assemblies are a functional unit of sharing and reuse the common language runtime. It provides the common language runtime with the information it need to be aware of type implementations. To the runtime, a type does not exist outside contest of an assembly. Assemblies are the fundamental part of the runtime.
In Physical term Assembly is a collection of physical files that owned by the assembly. These assemblies called static assemblies can include .NET Framework type (Interfaces and Classes) as well as the resources for the assembly (Bitmap, JPEG files, Resources files etc...). In addition, Common Language Runtime provides API’s that script engine use to create dynamic assemblies when executing script. These assemblies are run directly and never saved to disc, though we can save to disk.
Assembly concepts
An assembly forms a logical unit of functionality, a logical dll. An assembly forms the fundamental unit of Deployment, Version Control, Reuse, Activation Scoping and Security Permission. It is important to understand what an assembly is not. An assembly is not a unit of application deployment. It is a unit of class deployment, and these assemblies can be packed for deployment in several ways.
Assembly Contents
In general an assembly consists of for elements
1. The assembly manifests.
2. Metadata.
3. Microsoft intermediate language (MSIL).
4. A set of resources.
There are several ways to group these elements in an assembly. You can group all elements in a single physical file or it can be contained in several files.
1. Assembly Manifest
Assembly manifest contains Assembly Name, version number, culture, and strong name, list of all files, Type references, and referenced assemblies. Assembly manifest contain information on all items considered part of an assembly, this information is known as assembly metadata. Manifest indicates what items are exposed outside of the assembly and what items are accessible only within the current assembly's scope. Assembly manifest also contain collection of reference to other assemblies, these references are resolved at runtime base on the information stored in the manifest. The assembly's manifest contains all the information need to use an assembly.
All assemblies have a manifest and all application that use the runtime must be made up of an assembly or assemblies. The entire file make up the assembly must be listed in the manifest. Manifest can be sorted in several ways. For an assembly with one associated file, the manifest is incorporated into the Portable Executable (PE) file to form a Single file Assembly. A Multi file Assembly can be created with either the manifest as a standalone file or incorporated with one of the PE file in the assembly.
The manifest contain following information’s
1. Assembly Name
Contain a textual string name of the Assembly.
2. Version Information
It Consists of Major and Minor Version number, a Version and a build number. These numbers are used by the runtime enforcing version policy.
3. Shared Name Information
It contains the public key from the publisher and a hash of the file contains the manifest signed with the publisher private key.
4. Culture, Processor and OS Support
It contains information on the Culture, Processor and OS of Assembly support. For this release, the Processor and OS information is ignored by the runtime.
5. List of all Files in the Assembly
It consist of a hash of each files contained in the assembly and relative path to the file from the manifest file.
6. Type Reference Information
It contains the information used by the runtime to map a type reference to the file that contains its declaration and implementation.
7. Information on Referenced Assembly
It contains a list of other assemblies that are statically referenced by the assembly. Each reference includes the dependent assembly’s name, metadata and the public key if the assembly is shared.
A developer can also set custom assembly attribute in code. These attribute are informational only and are not used by the runtime in any way. Custom attribute includes.
1. Title: - provide a friendly name, which include spaces. For e.g. Name of an assembly may be comdlg, while assembly title would be Microsoft Common Dialog control.
2. Description: - A short description of the assembly.
3. Default Alias: - Provides a friendly default alias in case the assembly name is not friendly or a GUID.
4. Configuration Information: - consists of a string that can be set to any value for configuration information such as Retail or Debug.
5. Product Information: - such as Trademark, Copyright, Product, company etc.
2. Metadata
Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. Meta data is a binary Information describing your code that is either stored in a >NET Framework Portable executable (PE) file or in the memory. When your code is complied into a PE file, Metadata is inserted into one portion of the file while your code is converted to Microsoft Intermediate Language (MSIL) and inserted into another. Every type and members are defined and referred in file or assembly described in Metadata. When code is executed CLR run time loads the Metadata information into in-memory data structure that it references when it requires information about code class, members, inheritance etc. Runtime refection services are used to retrieve information from in-memory data structure.
Many key .NET Framework benefits are from the runtime usage of metadata. Metadata provide a common frame of reference that enables communication between the runtime, compliers, debuggers, and code that has been complied into MSIL. It enables the runtime to locate and load your code, generate native code at runtime, and provide memory management services. The runtime able to track which portion of your code is allowed to access and preventing it from assessing memory that it shouldn't. Metadata also helps the runtime and garbage collection keep the track of memory that will be released back into operating system that is no longer is needed. The information stored in the metadata also enables the CLR to enable security by tracking the access privileges that your code request and granted.
Microsoft Intermediate Language (MSIL)
It is also known as Common Intermediate Language. You can use any .Net compliers for compiling the .Net application and converted into MSIL. The main purpose of this Intermediate code formation is to have a platform independent code. You can run the MSIL code on any platform provided appropriate run time environments are installed on the specific platform you wish to run. The.NET compiler can generate code written using any supported languages and finally convert it to the required machine code depending on the target machine.
Types of Assembly
Mainly there are 3 Types of Assemblies
1. Private assembly- can be accessed only by single application
2. Public/shared assembly-can be accessed by multiple applications in a system.
3. Satellite Assemblies- Used for Multilingual application
Creating and Calling Private Assembly
Creating .dll for Private Assembly
1. Open MSVS then click the File->New ->Project, New Window will open, from that select Visual C# from Project Types and select "Class Library" from Templates, name the project as PrivareClassLibrary
2. Then new Class1.cs form will open and change the class name to MyClass, then write the below code in the form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrivateClassLibrary
{
public class MyClass
{
public string Display()
{
return "Hi-From Private Assembly";
}
}
}
3. Then click on Build->Build PrivateClassLibrary
4. Then go to your project saved folder->PrivateClassLibrary->Bin->Debug there you can see the .dll file ( PrivateClassLibrary.dll)
Calling Private Assembly
Select New project as mentioned above, click on WindowsFormsApplication instead of class library, change name as ClassLibraryCalling, then press ok. New window will open, and then Right Click the References in the Solution Explore-> Add Reference, new window will open, Click the Browse Tab, locate the PrivateClassLibrary.dll and click the file and the press ok. Now you can see the PrivateClassLibrary appeared in the Reference
Then select the form and add a button write this code in the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ClassLibraryCalling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrivateClassLibrary.MyClass obj = new PrivateClassLibrary.MyClass();
MessageBox.Show(obj.Display());
}
}
}
We are calling class object like PrivateClassLibrary.MyClass obj because we placed our class MyClass in PrivateClassLibrary Namespace
Creating and Calling Public Assembly
Creating .dll for Public Assembly
Private Assemblies are placed in Global Assembly Cache(GAC), the one of the main difference between public and private assembly is that Public assembly is do not copied to Bin/Debug folder of the application, it can be referred any application but only one copy is stored. In order to create a public assembly we need to do mainly three things
1. Create a strong name to Assembly
2. Associate strong name with assembly
3. Install the public assembly in GAC
Creating Assembly
1. Create a class files (use above same steps used in Private assembly) and name it as "PublicClassLibrary" and paste the following code in the class file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PublicClassLibrary
{
public class MyClass
{
public string Display()
{
return "Hi-From Private Assembly";
}
}
}
3. Then click on Build->Build PublicClassLibrary
4. Then go to your project saved folder->PublicClassLibrary->Bin->Debug there you can see the .dll file (PublicClassLibrary.dll)
5. Creating Strong Name
Open Visual Studio Command Prompt
In order to pace the dll file in the GAC we need to create a Strong Name for the assembly, Strong name is a combination of Private key and public key, for creating strong name click Start->Programs-> Microsoft Visual Studio 2008( or your version) -> Visual Studio Tools -> Visual Studio 2008 Command Prompt , then go to the Application folder where your class file is located. Then Type the following command sn –k dllfilename.key
sn –k PublicClassLibrary.key
Then you get a following message that “Key pair written to PublicClassLibrary.key “
You can see new file PublicClassLibrary.key is created at that folder.
6. Associate strong name with assembly
Open the PublicClassLibrary Application, Select PublicClassLibrary properties from project tab and new window will open with name PublicClassLibrary, from the signing tab(Left side-Bottom) check 'Sign the assembly' check box, select PublicClassLibrary.key file using 'Choose a strong name key file combo box'. Then close the window and Build the solution again using Build->Build Solution
7. Installing Assembly in GAC
GAC is a folder name Assembly in Windows, In order to place assembly in GAC, using Visual Studio Command Prompt go to the Bin/Debug folder where assembly (Dll) file is placed, Type
gacutil -i PublicClassLibrary.dll
After that you will get a message that “Assembly successfully added to the cache”. Now the assembly is placed in GAC and you can refer it to any other project.
Calling Public Assembly
Select New project as mentioned above, click on WindowsFormsApplication instead of class library, change name as PublicClassLibraryCalling, then press ok. New window will open, and then Right Click the References in the Solution Explore-> Add Reference, new window will open, Click the Browse Tab, locate the PublicClassLibrary.dll and click the file and the press ok. Then add a button in the form and write the following code in the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PublicClassLibrary.MyClass obj = new PublicClassLibrary.MyClass();
MessageBox.Show(obj.Display());
}
}
}
If you place an assembly in GAC, it will not create any copy of Assembly in Bin folder of Application.
Download This Program
Donload Private Assembly
Donload Public Assembly
In Order work Public Assembly program you need to place the Assembly in GAC as explained above
An assembly is a partially compiled code library for use in deployment, versioning and security. The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET assemblies may or may not be executable.Assemblies are a functional unit of sharing and reuse the common language runtime. It provides the common language runtime with the information it need to be aware of type implementations. To the runtime, a type does not exist outside contest of an assembly. Assemblies are the fundamental part of the runtime.
In Physical term Assembly is a collection of physical files that owned by the assembly. These assemblies called static assemblies can include .NET Framework type (Interfaces and Classes) as well as the resources for the assembly (Bitmap, JPEG files, Resources files etc...). In addition, Common Language Runtime provides API’s that script engine use to create dynamic assemblies when executing script. These assemblies are run directly and never saved to disc, though we can save to disk.
Assembly concepts
An assembly forms a logical unit of functionality, a logical dll. An assembly forms the fundamental unit of Deployment, Version Control, Reuse, Activation Scoping and Security Permission. It is important to understand what an assembly is not. An assembly is not a unit of application deployment. It is a unit of class deployment, and these assemblies can be packed for deployment in several ways.
Assembly Contents
In general an assembly consists of for elements
1. The assembly manifests.
2. Metadata.
3. Microsoft intermediate language (MSIL).
4. A set of resources.
There are several ways to group these elements in an assembly. You can group all elements in a single physical file or it can be contained in several files.
1. Assembly Manifest
Assembly manifest contains Assembly Name, version number, culture, and strong name, list of all files, Type references, and referenced assemblies. Assembly manifest contain information on all items considered part of an assembly, this information is known as assembly metadata. Manifest indicates what items are exposed outside of the assembly and what items are accessible only within the current assembly's scope. Assembly manifest also contain collection of reference to other assemblies, these references are resolved at runtime base on the information stored in the manifest. The assembly's manifest contains all the information need to use an assembly.
All assemblies have a manifest and all application that use the runtime must be made up of an assembly or assemblies. The entire file make up the assembly must be listed in the manifest. Manifest can be sorted in several ways. For an assembly with one associated file, the manifest is incorporated into the Portable Executable (PE) file to form a Single file Assembly. A Multi file Assembly can be created with either the manifest as a standalone file or incorporated with one of the PE file in the assembly.
The manifest contain following information’s
1. Assembly Name
Contain a textual string name of the Assembly.
2. Version Information
It Consists of Major and Minor Version number, a Version and a build number. These numbers are used by the runtime enforcing version policy.
3. Shared Name Information
It contains the public key from the publisher and a hash of the file contains the manifest signed with the publisher private key.
4. Culture, Processor and OS Support
It contains information on the Culture, Processor and OS of Assembly support. For this release, the Processor and OS information is ignored by the runtime.
5. List of all Files in the Assembly
It consist of a hash of each files contained in the assembly and relative path to the file from the manifest file.
6. Type Reference Information
It contains the information used by the runtime to map a type reference to the file that contains its declaration and implementation.
7. Information on Referenced Assembly
It contains a list of other assemblies that are statically referenced by the assembly. Each reference includes the dependent assembly’s name, metadata and the public key if the assembly is shared.
A developer can also set custom assembly attribute in code. These attribute are informational only and are not used by the runtime in any way. Custom attribute includes.
1. Title: - provide a friendly name, which include spaces. For e.g. Name of an assembly may be comdlg, while assembly title would be Microsoft Common Dialog control.
2. Description: - A short description of the assembly.
3. Default Alias: - Provides a friendly default alias in case the assembly name is not friendly or a GUID.
4. Configuration Information: - consists of a string that can be set to any value for configuration information such as Retail or Debug.
5. Product Information: - such as Trademark, Copyright, Product, company etc.
2. Metadata
Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. Meta data is a binary Information describing your code that is either stored in a >NET Framework Portable executable (PE) file or in the memory. When your code is complied into a PE file, Metadata is inserted into one portion of the file while your code is converted to Microsoft Intermediate Language (MSIL) and inserted into another. Every type and members are defined and referred in file or assembly described in Metadata. When code is executed CLR run time loads the Metadata information into in-memory data structure that it references when it requires information about code class, members, inheritance etc. Runtime refection services are used to retrieve information from in-memory data structure.
Many key .NET Framework benefits are from the runtime usage of metadata. Metadata provide a common frame of reference that enables communication between the runtime, compliers, debuggers, and code that has been complied into MSIL. It enables the runtime to locate and load your code, generate native code at runtime, and provide memory management services. The runtime able to track which portion of your code is allowed to access and preventing it from assessing memory that it shouldn't. Metadata also helps the runtime and garbage collection keep the track of memory that will be released back into operating system that is no longer is needed. The information stored in the metadata also enables the CLR to enable security by tracking the access privileges that your code request and granted.
Microsoft Intermediate Language (MSIL)
It is also known as Common Intermediate Language. You can use any .Net compliers for compiling the .Net application and converted into MSIL. The main purpose of this Intermediate code formation is to have a platform independent code. You can run the MSIL code on any platform provided appropriate run time environments are installed on the specific platform you wish to run. The.NET compiler can generate code written using any supported languages and finally convert it to the required machine code depending on the target machine.
Types of Assembly
Mainly there are 3 Types of Assemblies
1. Private assembly- can be accessed only by single application
2. Public/shared assembly-can be accessed by multiple applications in a system.
3. Satellite Assemblies- Used for Multilingual application
Creating and Calling Private Assembly
Creating .dll for Private Assembly
1. Open MSVS then click the File->New ->Project, New Window will open, from that select Visual C# from Project Types and select "Class Library" from Templates, name the project as PrivareClassLibrary
2. Then new Class1.cs form will open and change the class name to MyClass, then write the below code in the form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrivateClassLibrary
{
public class MyClass
{
public string Display()
{
return "Hi-From Private Assembly";
}
}
}
3. Then click on Build->Build PrivateClassLibrary
4. Then go to your project saved folder->PrivateClassLibrary->Bin->Debug there you can see the .dll file ( PrivateClassLibrary.dll)
Calling Private Assembly
Select New project as mentioned above, click on WindowsFormsApplication instead of class library, change name as ClassLibraryCalling, then press ok. New window will open, and then Right Click the References in the Solution Explore-> Add Reference, new window will open, Click the Browse Tab, locate the PrivateClassLibrary.dll and click the file and the press ok. Now you can see the PrivateClassLibrary appeared in the Reference
Then select the form and add a button write this code in the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ClassLibraryCalling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrivateClassLibrary.MyClass obj = new PrivateClassLibrary.MyClass();
MessageBox.Show(obj.Display());
}
}
}
We are calling class object like PrivateClassLibrary.MyClass obj because we placed our class MyClass in PrivateClassLibrary Namespace
Creating and Calling Public Assembly
Creating .dll for Public Assembly
Private Assemblies are placed in Global Assembly Cache(GAC), the one of the main difference between public and private assembly is that Public assembly is do not copied to Bin/Debug folder of the application, it can be referred any application but only one copy is stored. In order to create a public assembly we need to do mainly three things
1. Create a strong name to Assembly
2. Associate strong name with assembly
3. Install the public assembly in GAC
Creating Assembly
1. Create a class files (use above same steps used in Private assembly) and name it as "PublicClassLibrary" and paste the following code in the class file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PublicClassLibrary
{
public class MyClass
{
public string Display()
{
return "Hi-From Private Assembly";
}
}
}
3. Then click on Build->Build PublicClassLibrary
4. Then go to your project saved folder->PublicClassLibrary->Bin->Debug there you can see the .dll file (PublicClassLibrary.dll)
5. Creating Strong Name
Open Visual Studio Command Prompt
In order to pace the dll file in the GAC we need to create a Strong Name for the assembly, Strong name is a combination of Private key and public key, for creating strong name click Start->Programs-> Microsoft Visual Studio 2008( or your version) -> Visual Studio Tools -> Visual Studio 2008 Command Prompt , then go to the Application folder where your class file is located. Then Type the following command sn –k dllfilename.key
sn –k PublicClassLibrary.key
Then you get a following message that “Key pair written to PublicClassLibrary.key “
You can see new file PublicClassLibrary.key is created at that folder.
6. Associate strong name with assembly
Open the PublicClassLibrary Application, Select PublicClassLibrary properties from project tab and new window will open with name PublicClassLibrary, from the signing tab(Left side-Bottom) check 'Sign the assembly' check box, select PublicClassLibrary.key file using 'Choose a strong name key file combo box'. Then close the window and Build the solution again using Build->Build Solution
7. Installing Assembly in GAC
GAC is a folder name Assembly in Windows, In order to place assembly in GAC, using Visual Studio Command Prompt go to the Bin/Debug folder where assembly (Dll) file is placed, Type
gacutil -i PublicClassLibrary.dll
After that you will get a message that “Assembly successfully added to the cache”. Now the assembly is placed in GAC and you can refer it to any other project.
Calling Public Assembly
Select New project as mentioned above, click on WindowsFormsApplication instead of class library, change name as PublicClassLibraryCalling, then press ok. New window will open, and then Right Click the References in the Solution Explore-> Add Reference, new window will open, Click the Browse Tab, locate the PublicClassLibrary.dll and click the file and the press ok. Then add a button in the form and write the following code in the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PublicClassLibrary.MyClass obj = new PublicClassLibrary.MyClass();
MessageBox.Show(obj.Display());
}
}
}
If you place an assembly in GAC, it will not create any copy of Assembly in Bin folder of Application.
Download This Program
Donload Private Assembly
Donload Public Assembly
In Order work Public Assembly program you need to place the Assembly in GAC as explained above
No comments :
Post a Comment