Section 5.1. Assembly Version Number


5.1. Assembly Version Number

Every assembly has a version number. That number applies to all components (potentially across multiple modules) in the assembly. You typically specify the version number in the Visual Studio 2005 project settings, although you can also assign a version number during the link phase, using command-line utilities and switches or MSBuild.

To specify the version number using Visual Studio 2005, bring up the assembly properties, and open the Application tab. Click the Assembly Information button to display the Assembly Information dialog (see Figure 5-1).

Figure 5-1. Specify the assembly version in the Assembly Information dialog


Next to Assembly Version are four text boxes used to specify the assembly version. The Assembly Information dialog is merely a visual editor for a set of assembly attributes. These attributes are stored in the project's AssemblyInfo.cs file. The attribute used for the assembly version is AssemblyVersion. For example, the AssemblyVersion value corresponding to the settings in is:

     [assembly: AssemblyVersion("1.2.3.4")] 

The version number is recorded in the server assembly manifest. When a client developer adds a reference to the server assembly, the client assembly records in its manifest the name and the exact version of the server assembly against which it was compiled. If the client uses the class MyClass from version 1.2.3.4 of the assembly MyAssembly, the manifest of the client will record that the client requires version 1.2.3.4 of MyAssembly to operate and will contain this declaration:

     .assembly extern MyAssembly     {       .ver 1:2:3:4     } 

At runtime, .NET resolves the location of the requested assembly, and the client is guaranteed to get a compatible assembly. If a compatible assembly isn't found, an exception is thrown. The question is, what constitutes a compatible assembly? The rule of compatibility is straightforward: for a strongly named assembly (defined in the section "Strong Assembly Names"), a compatible assembly must have the exact same version number that the client's manifest requests. For a friendly named assembly, any assembly with the same friendly name is considered compatible.

5.1.1. Version Number Elements

The version number is composed of four numbers: the major version number, minor version number, build number, and revision number. Figure 5-2 points out these numbers, in order.

Figure 5-2. Breakdown of the assembly version number


Although you can assign any semantic to these numbers, there is a guideline or convention you should follow that conveys the meaning of version changes to the consumers of your assembly: a greater build number indicates a newer version of the same compatible assembly, and a greater revision number indicates some minor change (perhaps a minor bug fix) or changes made due to localization. As part of the product release procedures, you should verify that you have incremented the appropriate part of the version number, which reflects the nature of the new release.

The Assembly Information dialog also lets you specify the file version number. The file version number has no bearing whatsoever on .NET versioning; it is simply available for your custom needs if you want to apply proprietary semantics.


5.1.2. Providing the Version Number

The default version number provided by Visual Studio 2005 for new assemblies is 1.0.0.0. However, you can provide parts of the version number explicitly, or let Visual Studio 2005 generate them automatically. If you specify all four numbers explicitly, that will be the version number used. You can also provide * for the build and leave the revision number blank. This instructs the compiler to automatically generate build and revision numbers. For the build number, the compiler uses the number of days since January 1, 2000, local time. For the revision number, the compiler uses the number of seconds since midnight, local time, divided by two (without adjustment for daylight savings). A possible assembly version number generated by the compiler could be:

     1.2.1642.18000 

If you have some other schema for generating build numbers, you can use it and choose just to mask out the revision numbersimply specify the first three numbers manually and use a * for the revision number. The compiler then generates only the revision number, as just described.

.NET has an assembly attribute called AssemblyInformationalVersion:

     [assembly: AssemblyInformationalVersion("1.2.3.4")] 

This attribute is available as a complementary service for you to store a custom version number. It is ignored when .NET is trying to resolve the assembly version.


Obtaining the Assembly Version

You can obtain the assembly version programmatically, using the GetName( ) method of the Assembly type. GetName( ) returns an instance of the AssemblyName class. The AssemblyName class has a public property called Version, of the type Version:

     public sealed class Version : ICloneable,IComparable,                                   IComparable<Version>     {        // Constructors        public Version(  );        public Version(int major, int minor);        public Version(int major, int minor, int build);        public Version(int major,int minor,int build,                                                  int revision);        public Version(string version);              // Properties        public int Build{get;}        public int Major{get;}        public int Minor{get;}        public int Revision{get;}     } 

You can either access individual version numbers or convert it to a string:

     Assembly assembly = Assembly.GetExecutingAssembly(  );     Version version = assembly.GetName(  ).Version;     Trace.WriteLine("Version is " + version); 




Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net