When being applications, ideally you want your assemblies to be labelled with the associated build. This will allow you to keep track of your versions, useful for bug reports, and for .NetCLR to correctly identity which version of an assembly to use. There are some disadvantages for doing this, one being you lose the flexibility to xcopy updated assemblies without having to recompile everything, this is the release why Microsoft keep versions at 2.0.0.0 (or 10.0.0.0 in the case of SQL Server 2008).
But, I want my assemblies to be labelled, as such I want to use the build counter TeamCity keeps track of when it builds my applications and have that as the value for my AssemblyVersion property. TeamCity is my current continuous integration server which uses MSBuild to compile my application, it is always best to take the build number from a CI server as it acts as a centralised point for revisions.
Within the TeamCity control panel, on the general settings page for your build configuration, you have two important boxes. The first box defines the build number format, there is then a place holder as {0} which is where TeamCity inserts it counter value. This counter value is within the second text box. Every time your project is built, this is increment. The counter contains the value which will be used for the next build.
We now have our build number in the format we want (in this case 1.0.0.28), but we need to be able to access this when the application is being built. As it happens, TeamCity provides this number within an environment variable BUILD_NUMBER which we can use within our build script via the $() syntax, the code below takes the number from TeamCity and puts it inside another variable called Version.
I then need to update my AssemblyInfo.cs file with this build number. The AssemblyInfo file contains all of the metadata about the assembly which is included when it is built. I just use a MSBuild community task to rewrite my complete AssemblyInfo.cs file with all of the required information.
AssemblyTitle=”DLRHost.Engine”
AssemblyDescription=”DLRHost Engine”
AssemblyConfiguration=””
AssemblyCompany=”Ben Hall – Blog.BenHall.me.uk”
AssemblyProduct=”DLRHost”
AssemblyCopyright=”Copyright (c) Ben Hall, 2008″
AssemblyTrademark=””
ComVisible=”false”
CLSCompliant=”true”
Guid=”d038566a-1937-478a-b5c5-b79c4afb253a”
AssemblyVersion=”$(Version)”
AssemblyFileVersion=”1.0.0.0″ />
Notice at the bottom, as the AssemblyVersion I am using my variable which contains the TeamCity build number. After the assembly has been built, if we load the assembly in Reflector you can see all of the metadata included, including the correct Version.
In summary, that is now you can attach your current build number to your assemblies with TeamCity.
Does this task make checkIn of modified AssemblyInfo file to TFS?
A bit late to the party, but this was really helpful for me – I’ve just sorted it on my build server along with the vcs check in version, and it’s looking sweet!