Understanding .NET Assemblies

Discussion in 'ASP.NET' started by MinalS, Feb 20, 2015.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    32
    Trophy Points:
    0
    A namespace is imported in the source code. An assembly is referred by the compiler switch during the deployment of an application. The assembly contains the following components.
    1. Assembly manifest
    2. MSIL source code
    3. Type metadata
    4. Resources
    1. Assembly manifest

    The manifest unit is used for storing the information about the programming units. The manifest contains collection of data known as metadata. The manifest data can be stored in the .exe file or .dll file. The manifest contains the following information.
    1. Assembly Name: It represents the text string specifying the assembly name.
    2. Version number: It refers the version number of application.
    3. Culture: It represents the information on the culture or language supported by an assembly.
    4. String name information: It represents the public key issued
    5. Information on referenced assemblies: It shows the list of assemblies that are statistically referred to another assembly.
    6. Type reference information: It states the information used at the runtime for mapping the type reference to the file containing the declaration and implementation.
    7. Files in assembly: It information about the files in an assembly is mentioned.
    2. MSIL source code

    The source code is compiled to MSIL through the compiler. MSIL is platform independent, the CPU instructions that are executed in environment supported by the framework. The information about loading, storing, initializing, and calling the methods is provided. The arithmetic, logical, memory access, exception handling operations is performed.

    3. Type metadata

    The type metadata describes the format of the types stored in an assembly. The information about external types is referred by an assembly. The CLR loads the metadata to memory and references it for retrieving the information about the classes, members, and inheritance of the source code.

    4. Resources

    The .NET framework contains a main assembly that has embedded resources. The main assembly has the embedded resources. If the resource is not available in the localized satellite assembly, the Resource Manager class loads the data in hierarchical manner.

    Assembly Types



    The assemblies are categorized as shared assemblies and private assemblies depending on their location and accessibility.

    1. Shared Assembly

    The shared assembly is collection of types and resources. The assembly is deployed in the Global Assembly Cache ( GAC ). The single copy of the shared assembly is used by several applications on a single machine.

    The System.Data, System.Drawing, etc are the shared assemblies that are imported and can be used by all the .NET applications.

    Consider an example to demonstrate the use of the shared assembly in .NET.

    The class library is created in the Windows application. The following steps are used for creating the class library in .NET application.

    1. In Visual Studio application, select File, Click New, Project from the menu list.

      [​IMG]
    2. Select Installed, Templates, Visual C# option.
    3. Click on the Class Library template from the pane

      [​IMG]
    4. Add the name of the application, location of the application.
    5. Click OK, and close the application.
    6. In the Class1.cs file, add the following code.
    Code:
    
    using System;
    using System.Collection.Generic;
    using System.Text;
    namespace SharedData
    {
    	public class Class1
    	{
    		public string Message ( String user )
    		{
    			return ( “ The user name is”+user);
    		}
    	}
    }
    
    
    Strong names in an assembly

    The strong names known as Globally Unique identider, ( GUID ) and the component object model are used as ID’s that uniquely identifies the assembly. For creating a strong name for the assembly, use the sn.exe command.

    Perform the following steps for creating the strong name in an assembly.
    1. Click Start, All Programs, Microsoft Visual Studio 2012, Visual Studio Tools, select Developer Command Prompt.
    2. Type the following command in the prompt window.
      Code:
      	sn –k Bkey.snk
      
      The command creates a strong name key pair file in the current directory.
    3. Add the key pair file in the SharedObject application. Right click the name of the application in the Solution Explorer window. Select Add, Existing Item.
    4. Click Add button from the Item dialog box.
    5. The AssemblyInfo.cs file contains the key pair file created by the user.
    6. Open the Developers Command Prompt, navigate to the SharedObject.dll file.
    7. Type the following command in the command prompt for visual studio.
      Code:
      gacutil /i SharedObject.dll
      
    Private Assemblies

    The private assemblies are private to the clients like the DLLs. If the no of applications using the library assembly are small, user can add them in the client applications directory and not in the GAC. These assemblies are known as private assemblies.

    The private assemblies can be managed easily. They can be installed and uninstalled without affecting other applications. Strong names are not needed in the private assemblies.

    Consider the example containing two classes as Class1 and Class2. They have methods for displaying values. Perform the following steps for creating the private assembly.
    1. Create a Class Library and save it with an appropriate name.
    2. Add two classes and each containing a method.

    3. Code:
      using System;
      using System.Collection.Generic;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace PrivateAssembly
      {
      	public class Class1 
      	{
      		public void Show ( string msg )
      		{
      		
      			MessageBox.Show( msg );
      		}
      	}
      	
      	public class Class2 
      	{
      		public void Show( string msg )
      		{
      			MessageBox.Show ( msg, “ “ , MessageBoxButtons.OK);
      		}
      	}
      }
      
    4. Build the application using the Build Solution option
    5. Add a new forms project in the assembly solution.
    6. Right click the project in solution explorer; select the Set as startup project option from the menu.
    7. Add the dll reference file in the project. Right click in the solution explorer window, select Project, Add Reference.
    8. Add a Button control on the Form, set the text property to Display.
    9. In the Click event of the Button control, add the following code.
      Code:
      private void button1_Click ( object sender, EventArgs e )
      {
      	c1.Display ( “This is an introduction to the Assembly “);
      }
      
    10. Click the Build solution option and the output is displayed.
    Multifile assembly

    The multifile assembly is distributed across different files work by creating the application. The following steps are performed for creating the multifile assembly.
    1. Open the Notepad in the system and add the following code.
      Code:
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      namespace Class1
      {
      	class Class1
      	{
      		public void Rating ( string username )
      		{
      			System.Console.WriteLine(“ The Rating 1 is assigned to the user”+username);
      		}
      	}
      }
      
    2. Save the file as Class1.cs and create another file as Class2.cs. Add the following code in the file.
      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      namespace Class1
      {
      	class Class2
      	{
      		public void Rating ( string username )
      		{
      			System.Console.WriteLine(“ The Rating 2 is assigned to the user”+username);
      		}
      	}
      }
      
    3. Create a simple client application and add the following code.
      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      namespace SimpleApplication
      {
      	class SimpleClient
      	{
      		public static void Main ( )
      		{
      			Class1.Class1.c1 = new Class1.Class1();
      			c1.Rating (“Nick”);
      			Class2.Class2 c2 = new Class2.Class2();
      			c2.Rating (“Mark”);
      		}
      	}
      }
      
    4. Open the Visual Studio Command Prompt, navigate to the folder where user has saved all the files. Type the following command.
      Code:
      csc /t:module Class1.cs
      
    5. Type the following code to compile the Class2.cs file.
      Code:
      csc /t:module Class2.cs
      
    6. Type the following command to compile the SimpleClient.cs.
      Code:
      csc /t:module SimpleClient.cs /addmodule:class1.netmodule, class2.netmodule
      
    7. The three modules are combined together for creating an assembly using the assembly linker utility.
      Code:
      al Class1.netmodule Class2.netmodule SimpleClient.netmodule /main:SimpleClient.Main / out:MultiFileAssembly.exe /target.exe
      

    Attributes of an assembly



    Attributes are the keywords declarations added to annotate the elements. The elements can be types, fields, methods, and properties in an assembly. They are saved with the metadata of the assembly. They are used to describe the code at runtime. The extra description information of the metadata is added and can be extracted at runtime using the reflection services.

    The data can be serialized; characteristics that enforce the data security, reflection services can be done through assemblies. Various attributes that are supported by the assembly are as listed below.

    1. Assembly Identity Attributes

    The assembly identity attributes provide an identity to an assembly using the full name. They are required when the assemblies are referred in the source code of an application.

    The three identity attributes that are used along with the strong name are as mentioned below:
    1. AssemblyCultureAttribute: It specifies the culture of an assembly.
    2. AssemblyFlagsAttribute: It specifies whether an assembly can be executed multiple times simultaneously on the same machine.
    3. AssemblyVersionAttribute: It specifies the numeric value for determining the version number of the assembly.
    2. Informational Attributes

    The informational attributes contains additional information about an assembly. It contains values as name of the company, product name for the assembly it is provided.

    The different informational attributes are as mentioned below:
    1. AssemblyCompanyAttribute: It specifies the string value for the company name.
    2. AssemblyFileVersionAttribute: It specifies the string value for the file version
    3. AssemblyCopyRightAttribute: It specifies the string value for the copyright information
    4. AssemblyProductAttribute: It specifies the string value to check the .NET application for the assembly it belongs.
    5. AssemblyTradeMarkAttribute: It specifies the string value for the trademark information.
    3. Assembly Manifest Attributes

    The assembly manifest attributes provide the information in the assemblies manifest. The values such as title, description, alias, and configuration are defined.

    Some of the assembly manifest attributes are as mentioned below:
    1. AssemblyConfigurationAttribute: It specifies the string value providing the debugging information.
    2. AssemblyDescriptionAttribute: It specifies the string value providing the description of the assembly
    3. AssemblyTitleAttribute: It specifies the string value to provide the name of an assembly.
    4. AssemblyDefaultAliasAttribute: It specifies the string value to provide the alias name for the assembly.
    4. Strong Name attributes

    The name of the assembly is provided using the strong name attributes. Some of the strong name attributes are as mentioned below:
    1. AssemblyDelaySignAttribute: It specifies the Boolean value indicating the Delayed Signing option used to sign the assembly.
    2. AssemblyKeyFileAttribute: It specifies the string value to provide the file name containing the public key.
    3. AssemblyKeyNameAttribute: It specifies the key container containing the key pairs that are passed as parameters.

    Assembly Signing



    There are two ways to sign an assembly using strong names and using the SignTool. When the user signs an assembly through the strong name, a public key encryption is added to the file containing the assembly manifest. Signing an assembly using SignTool provides a strong name and digital signature to the assembly. It requires a publisher to show the identity to the third party and obtain a certificate.

    SignTool provides authenticity and integrity through the digital signatures and certificates. It performs functions like hash verifications, strong naming, signing for ensuring the assembly is safe.

    It provides a command line tool for digitally signing files. The following command is used for signing in an assembly.
    Code:
    signtool [ command ] [ options ] [ file_name ] 
    
    where,

    command is the command flag for specifying the operation to performed on a file.
    options provides a option flag to modify a command flag.
    filename is used to specify the location of the file to be signed.

    Delayed Signing

    It is a technique where assembly is partially signed in the deployment phase. It protects and controls the private key of an assembly. It is used only in the process of deployment. It is useful when the developers from other companies need them.

    Lets understand the concept the delayed signing in .NET. Perform the following steps for delayed signing technique.
    1. Create a new class library project and add the following code.
      Code:
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      namespace Assembly1
      {
      	public class class1
      	{
      		void Meesage ()
      		{
      			System.Windows.Forms.MessageBox.Show(“Delayed siging assembly”);
      		}
      	}
      }
      
    2. Click Start, All Programs, Visual Studio 2012, Visual Studio Tools, Developer Command Prompt, change the directory to the appropriate folder.
    3. Add the following command in the command prompt.
      Code:
      sn /k Key1.snk
      
     
    Last edited by a moderator: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice