C#-The Language : A Step Ahead Series-Part-I

C#-The Language : A Step Ahead Series-Part-IV


This tutorial is a part of C#:2005-Beginners A Step Ahead Series book. Which tells about the assemblies:


Introduction


This tutorial will deal with the great feature of assemblies from .NET. Before, this lets remind older days. Earlier you have component objects DLL to favor you for more flexibility of program and security. But, that was not right for every time and everywhere. Also with the same you have faced a problem called DLL Hell.


What is DLL Hell?


Before C#, you have COM objects. These objects faced a problem which is called DLL Hell, actually what is this? In COM objects you need to register them with system's registry which is dependent on registry. Suppose, you have a DLL file registered with a system and want to register it with an amended edition then there is a problem which is known as DLL hell. These problems arose because all COM objects were depended on registries. To the reply for the same Microsoft invented Assemblies, which are free from registry or independent from registry.


Assemblies


Now, in simple words assemblies are same as earlier COM objects but these are independent from registration and containing manifest. Further, manifest contains the metadata information of the Assembly’s data that’s why assembly is called self describing.


In other words An assembly is a logical unit of deployment containing PE (portable executing files) files like DLL and EXE. So, one assembly can be a single DLL file or it contains more files like DLL, resources, bitmaps and exe etc.


An assembly is completely self-describing. An assembly contains metadata information, which is used by CLR for everything from type checking and security to actually invoking the components methods. As all information is in the assembly itself, it is independent of registry. This is the basic advantage as compared to COM where the version was store din registry.


Also, multiple versions can be deployed side by side in different folders. These different versions can execute at the same time without interfering with each other.


Assemblies can be private or shared. For private assembly deployment, the assembly is copied to the same directory as the client program that references it. No, registration is required for private assembly. For shared assembly deployment there is little fancy installation required discussed after few more words.


Assembly Features


There are following points to favoring it:



  • Assemblies are self describing

  • Same assembly contains multiple versions

  • Assembly deployment is very easy


Assemblies are logical units of physically group types that mean these contain
more than one files.



Namespace


Simple, namespace is logically group types, like System.Web.UI logically groups contains UI related features.


How to view an Assembly


To view the assembly means to view the IL code, ILDASM converts the whole exe or dll into IL Code. To start the same:

  • Go to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin and then double click on ildasm.exe

  • Open SDK command prompt and type ildasm

  • Now in ildasm open assembly File -> Open [or ctrl+O]

  • Double click on the MANIFEST

  • If you want to view mathclass, just double click on it

  • For further information about the methods just do the same as you did in above case


What is MANIFEST


This is a very important part of an assembly. It describes the full information of an assembly, so, in general words it contains metadata of an assembly. Manifest is a container of metadata of an assembly, which contains the followings:



  • Name, version info, culture info etc.

  • Identity of security

  • Scope of assembly

  • Resolve references to resources and classes.



Private and Shared Assemblies


There are two types of assemblies Private and Shared. Private assemblies are lying in their individual folder, where shared assemblies are lying in GAC [Global Assembly Cache].


Creating a Private Assembly


In Visual Studio .NET there is always an assembly whether you choose a class library project or an exe project. Now, let's start some practical work.


  • Start Visual Studio 2005

  • Choose a new C# project [File -> New Project]

  • From template pane choose Class Library Template

  • Set the Location : F:\myWrittings\CSharpBook\Source Codes\

  • Name : privateAssembly

  • Check full code of private assembly.
  • In Solution explorer change class name from Class1 to mathclass

  • Now right click on mathclass and click on view code

  • Change the name of namespace from privateAssembly to CSharp.AStepAhead.privateAssemby

  • After writing codes of the assembly, now you have to provide some information to your assembly as follows:


  • using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("privateAssembly")]
    [assembly: AssemblyDescription("This is an Example of Book: C#-A Step Ahead Series")]
    [assembly: AssemblyConfiguration("Free I.T. Education Series - A Step Ahead")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("A Step Ahead Series")]
    [assembly: AssemblyCopyright("Copyright © A Step Ahead 2007")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components.  If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]
    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("d5389a36-3aa1-4e22-b3df-4cacd5e96531")]
    //
    //      Major Version
    //      Minor Version
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Revision and Build Numbers
    // by using the '*' as shown below:
    [assembly: AssemblyVersion("1.0.*")]

  • For above, you have to click on AssemblyInfo.cs under properties from Solution Explorer

  • The above information tells about the assembly name, title its short description and company who had developed it.

  • For more detail check table assembly attributes.
  • Write codes to provide the working for client application.

  • Now build solution from Build -> Build Solution or Ctrl + Shift + B

  • If build succeeded, it means you have created an assembly

  • You can check the assembly using ILDASM


Testing a Private Assembly


Lets start to create a client application for privateassembly, Open a notepad andwrite following code [till now you have not created any client application using visual studio, you will do the same very soon in ASP.Net section]. For this example lets create a client application using Console application from templates pane [windows applications are beyond the scope of the book].


  • In Solution explorer change the class name from Program to mathClassClient

  • Using private assembly.
  • Add CSharp.AStepAhead.privateAssembly namespace

  • Now, important step is to add assembly reference, right click on the project in solution explorer and click on the Add Reference [you can choose the same from Project menu]

  • Create an object of mathClass Class

  • Build the project or press F5

  • Important
    :Version information of an assembly is stored in its MANIFEST
    : Concept of Versioning is applicable only to GAC [Global Assembly Cache], because private assemblies are lying in their individual folders
  • Output of Client application :


Important:
1. Revise source of above project [privateAssembly project] to understand the code. In this project, you saw that there is nothing extraordinary operations, just accepting integer values and on the behalf of these values result will retaining by overriding ToString() method. Yes, you can override this virtual method as per your requirement, as done in this example.
2. Now, in Client application, this is a Console Based application project, as you are going for Asp.Net so, Windows applications are beyond the scope of the book.


Creating a Public Assembly


There are certain circumstances when you require shared assemblies like Security purpose, availability to others of the same assembly etc.
The shared assemblies are placed in GAC [Global Assembly Cache].



  1. Create a Class Library Project

  2. Generate Strong Names [Public / Private Key pair]

  3. Supply Assembly information
    [refer to Assembly Information]

  4. Assign .sn file

  5. Build project

  6. Place the assembly in GAC


Important
You can also convert a private assembly to shared assembly follows steps to create a shared assembly from step no. 2.

Strong Names Concept


We need strong names to generate shared assembly because GAC needs the same. To generate a strong name key pair do follow the below mentioned point(s):

  • Start the Visual Studio command prompt

  • Create a file using sn.exe

  • At this stage, if you want to see the file you can open it with any write editor like notepad.

  • As we have a strong key pair files, now its time to sign the assembly with this SNK file.

  • To do the same open the assembly Chose Project -> Project properties

  • Click on the Signing

  • Sign the assembly by choosing a string key file name

  • Here you can also create a new strong name key pair

  • The final step towards this is to place a shared assembly into GAC


    • To do the same there are two ways, you can either use Microsoft Installer Package

    • Or use gacutil.exe from SDK command prompt [open the command prompt and type as : gacutil –i sharedAssembly.dll


    Important
    1. Use gacutil -i sharedAssembly.dll - To Install an assembly into GAC
    2. Use gacutil -u sharedAssembly.dll - To Uninstall an assembly from GAC
    3. Use gacutil -l sharedAssembly.dll - List available assemblies in GAC


  • You can View from Windows path C:/Windows/Assemby/


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: