.Net: Assemblies (Part – 4)

As of now we have created hello assembly and testapp application. testapp application will call the function SayHello (it just prints “Hello, World!” on the console) resides in hello assembly. We have created strong name for hello assembly and installed it into Global Assembly Cache (GAC). hello assembly is the shared assembly and any application can use this assembly.

Now we will extend our hello assembly by adding another method to it. There are several questions will arise while developing new hello assembly.

  • When we deploy new hello assembly into GAC, does it replace old assembly?
  • Does testapp.exe application will work with new hello assembly?
  • Does it required to re-build testapp application?
  • How to tell to testapp application to use new hello assembly?

Don’t worry, we will address all these questions in these articles.

Usually when we extend an existing assembly by adding a new feature to it or modify the existing feature, we should release it as a new version to an existing assembly. It will be easy to maintain the releases if we maintain appropriate version numbers to our assemblies.

Lets take our hello assembly. The version number of hello assembly is 0.0.0.0. We are not explicitly provided any version number to our hello assembly. So, it is 0.0.0.0. Now we are going to add a new method to it. We need to provide a new version number to hello assembly. What version number we need to put? Usually the assembly version number is formed with 4 parts:

<major number>.<minor number>.<build number>.<revision number>

<major number>Major changes are done in the assembly and usually the changes are incompatible with older versions.

<minor number> – If the changes are minimal, assembly has to release as a minor release.

<build number><revision number> – These are updated whenever build happened to the assembly. The changes are not impacted any assembly’s functionality.

As mentioned, our hello assembly version number is 0.0.0.0; all major, minor, build and revision number are 0. Usually when we release the assembly to outside world, we should not release as 0.0.0.0. Provide valid version number, usually it starts with 1.0.0.0.

Lets provide version information to our hello assembly. To add version information we need to add following line to our hello assembly’s code.

[assembly: AssemblyVersion("1.0.0.0")]

Here AssemblyVersion is an attribute. Attributes are used to alter the behavior of an element. This attribute is applied on assembly program element. Hence prefixed with assembly. If the attribute is applying on program elements assembly or module; that line must be enclosed with in square brackets (“[]”).

Along with version information, we will add following attributes also to hello assembly.

[assembly: AssemblyTitle("Hello Assembly")]
[assembly: AssemblyDescription("Hello Assembly")]
[assembly: AssemblyFileVersion("1.0.0.0")]

AssemblyTitle attribute is used to provide friendly name to the assembly. AssemblyDescription to provide description to the assembly and AssemblyFileVersion is to define the version of the file. Observe that, this is different than AssmblyVersion attribute which gives the version number of the assembly.

And we will add one simple function Display to extend the functionality of it. Display function simply displays the message which is passed through its argument.

After we add all this code our hello.cs file looks like below:

// hello.cs
//
using System;
using System.Reflection;

[assembly: AssemblyTitle("Hello Assembly")]
[assembly: AssemblyDescription("Hello Assembly")]	
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

namespace CodeSteps
{
    public class Hello
    {
        public void SayHello()
        {
            Console.WriteLine("Hello, Universe!");
        }

        public void Display(String message)
        {
            Console.WriteLine(message);
        }

        public static void Main()
        {
            Hello hello = new Hello();

            hello.SayHello();
        }
    };
};

Observe that in SayHello function, we have modified the code to display “Hello, Universe!” text instead of “Hello, World!” text.

Lets compile hello.cs. Below is the command:

csc hello.cs

The compiler will generate hello.exe file. Run hello.exe from command prompt. It will display “Hello, Universe!” message. Perfect! our assembly is working fine.

So, what to do next? We will discuss it in our next article.

..

1 comment for “.Net: Assemblies (Part – 4)

Leave a Reply