Jan 4, 2009
Assembly versioning: How to include the Subversion revision number
When you build a C# project in Visual Studio, the version of the generated assembly comes from the AssemblyVersion attribute applied to the assembly. Often you want to update this version number when your code changes.
In the default project setup, this attribute is specified in AssemblyInfo.cs in the Properties directory. The version number consists of four parts, nominally: major version, minor version, build, revision. If you specify an asterisk for the build or build & revision parts, the compiler will update the version number at each build (based on the build time).
If you’re project is under source control, it can make more sense to link your version numbers to the version of your code at the time it was built. If you’re using Subversion, you can use the subwcrev tool from TortoiseSVN to generate the version number:
- Remove the AssemblyVersion attribute from the AssemblyInfo.cs file:
[assembly: AssemblyVersion("1.0.0.0")].Note: if you want AssemblyVersion and AssemblyFileVersion to be the same, you do not need to specify AssemblyFileVersion at all.
- Create a new VersionInfo.cs.tmpl code file in the Properties folder. (Try adding in the project root, and dragging it into the Properties folder). Put the AssemblyVersion attribute in the file. For example:
[assembly: AssemblyVersion("1.0.0.$WCREV$")]
$WCREV$ will be replaced with the highest Subversion revision number of all files in the project at each build.
- Add a pre-build event to the project (all on one line):
subwcrev "$(ProjectDir)." "$(ProjectDir)\Properties\VersionInfo.cs.tmpl" "$(ProjectDir)\Properties\VersionInfo.cs"
Make sure that C:\Program Files\TortoiseSVN\bin is on your PATH.
- Edit the csproj file. Look for: <None Include="Properties\VersionInfo.cs.tmpl"/>
Add the following after it: <Compile Include="Properties\VersionInfo.cs"/>
- Build the project. The VersionInfo.cs file will have been generated. Add it to the Subversion ignore list, as you do not want it to exist in the repository.
If you want the version number to change with each build, try: [assembly: AssemblyVersion("1.0.$WCREV$.*")].
Build servers
For this to work on a build server, you need to make sure that your code is being built inside a working copy. For example, in TeamCity you need to set the VCS checkout mode to Automatically on agent in the version control settings. (Thanks, Eugene Petrenko)
Update 8 Feb 2010: Added section about build servers. Also, updated pre-build command as per Tricky Dicky’s comment below)
This tip was very helpful Bruce, however, I had to enusre that the VersionInfo.cs entry in the csproj file was above AssemblyInfo.cs as it seemed to cause an issue with the compiler looking for the Assembly Attribute reference. All fixed now and works great.
Many thanks.
PS I’m using VS2008, so don’t know if that was causing the build order issue.
Great tip! Really useful. Thanks for sharing.
As well as Iain’s note, I had to change my csproj file Pre Build Step as follows – note the period after the first ProjectDir and also the speech marks to enclose paths with spaces:
subwcrev “$(ProjectDir).” “$(ProjectDir)Properties\VersionInfo.cs.tmpl” “$(ProjectDir)Properties\VersionInfo.cs”
Nice, but have you thought of putting revision into AssemblyFileVersion instead – see KB556041 and
http://stackoverflow.com/questions/1035284/sharepoint-features-how-can-i-use-wildcard-assembly-versioning/103541