When talking about Version assemblies you would usually interact with AssemblyVersion, AssemblyFileVersion, AssemblyInformationalVersion Atrributes. Ability to successfully version assemblies is important while testing, debugging and deploying your assemblies. Why? Because it allows you and your team to inspect and debug same version of assemblies on one hand and to know what version of assemblies is are your customers using when talking about future requirements and current problems.
AssemblyVersion and AssemblyFileVersion
attributes
Can be manually located in your Solution explorer:
AssemblyInfo.cs file you can manually change values of a few Attributes. But now we will take look at AssemblyVersion and AssemblyFileVersion attributes. Both these Attributes provide default value in string format. Default values should appear like this when you initially create project:
1 2 |
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] |
Number values inside string have specific meaning:
- value -> major value. Value should be changed when changes to your program are noticeable, like major User interface and under the hood changes
- value -> minor. increment this value when adding new capabilities to your Application.
- value -> build number. Obviously this value should be incremented when running a build
- value -> revision. Value that you should increment when choosing which build version to deploy
Build number and revision can be automated by using asterisk(*). Just change strings too look like this and their values will be incremented automatically after each build:
1 2 |
[assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyFileVersion("1.0.*")] |
If you are developing small scale application it is best to change minor and major values manually.
AssemblyVersion attribute is used by other assemblies. When value of this attribute is changed, all assemblies that reference it must change value to match current value to ensure that communication between them will proceed uninterrupted.
AssemblyFileVersion attribute is used for deployment (when you ship assemblies to end user). You always need to know which version of assembly is shipped to your customer so that you can assist them, detect bugs and plan future changes.
AssemblyInformationalVersion
Usually you would not like to bore or even worse scare customers by displaying version of your Application in format like 8.56.7520.1459. You will want to display assembly version in user friendly format like 8.56 RC or 8 Orange, or any other format that will appeal to end user. This is where AssemblyInformationalVersion steps in. We can declare AssemblyInformationalVersion inside AssemblyInfo.cs file.
1 |
[assembly: AssemblyInformationalVersion("3.1 (Demonstration)")] |
Nice. Please show me how to access these values.
First of. make sure that your AssemblyInfo.cs files has following values:
1 2 3 |
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("2.0.0.0")] [assembly: AssemblyInformationalVersion("3.1.0.0 (Demonstration)")] |
And inside main Method put following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// Access Attributes 1st approach string assemblyVersionInfoValue = Assembly.GetExecutingAssembly().GetName().Version.ToString(); string assemblyFileVersionInfoValue = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; string assemblyInformationalVersionValue = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion; Console.WriteLine("AssemblyVersion attribute value: {0}", assemblyVersionInfoValue); Console.WriteLine("AssemblyFileVersion attribute value: {0}", assemblyFileVersionInfoValue); Console.WriteLine("AssemblyInformationalVersion attribute value: {0}", assemblyInformationalVersionValue); Console.WriteLine("..."); // Access Attributes 2nd approach Assembly assembly = Assembly.GetExecutingAssembly(); Attribute[] attributes = Attribute.GetCustomAttributes(assembly); foreach (Attribute attribute in attributes) { if (attribute is AssemblyVersionAttribute) { AssemblyVersionAttribute a = (AssemblyVersionAttribute)attribute; Console.WriteLine("Name: {0}, version {1}", a, a.Version ); } if (attribute is AssemblyFileVersionAttribute) { AssemblyFileVersionAttribute a = (AssemblyFileVersionAttribute)attribute; Console.WriteLine("Name: {0}, version {1}", a, a.Version); } if (attribute is AssemblyInformationalVersionAttribute) { AssemblyInformationalVersionAttribute a = (AssemblyInformationalVersionAttribute)attribute; Console.WriteLine("Name: {0}, version {1}", a, a.InformationalVersion); } } |
This post has thought you how to version assemblies and was first post in Manage assemblies topic. Now we will continue to topic on how to strong name your assembly.