Surendra Sharma

Surendra Sharma

Search This Blog

Friday, June 28, 2013

How to get autoincrement assembly version for ASP.NET website

In ASP.NET, developer can not assign assembly version to web site. However they can get assembly version info from console, windows, class library, web applications.

There is one way to get auto increment for assembly version for website.
Create blank solution, add web site project into it.

Add one class library to this solution from File -> Add -> New Project -> Class Library



Write a method which return assembly version of this class library as below

Open “AssemblyInfo.cs” file and paste following code

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("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright ©  2013")]
[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("e6761a90-b4a8-42cf-a455-c47a240dd8eb")]

// Version information for an assembly consists of the following four values:
//
//      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("2.0.*")]

namespace ClassLibrary1
{
    public partial class AssemblyInfo
    {
        /// <summary>
        /// Get version numbers from Assembly
        /// </summary>
        /// <returns></returns>
        public static string GetAssemblyVersion()
        {
            var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
        }
    }
}

Add reference of this class library in website project.

Write following code to get assembly version.

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Version " + ClassLibrary1.AssemblyInfo.GetAssemblyVersion();
}



Now everytime you rebuild your solution, you will get different assembly version.

No comments:

Post a Comment