Search Wiki:
Resource Page Description
MSBuild task to automatically update the Assembly Version number.

Project Goal


In .NET (C#) projects you can set the version number for the DLL in the Properties\AssemblyInfo.cs. This shows up under the file's details in Windows:
FileDetails.jpg

There is a nifty msbuild task that was distributed by Microsoft a while back called "AssemblyInfoTask" (I think it came with one of the Team Foundation Server releases) that can automatically set it to the current date which is very handy to quickly find out which version of a particular DLL is at a customer site. However there are a few issues with the strategy:
  • because the version number is a 16-bit number it has some issues with assemblies built after 2007 (there is a work around for it - as you can see I only have month and day)
  • it updates AssemblyInfo.cs (no easy way to customize that) which is usually under version control... so this causes a lot of unnecessary commits and conflicts (trivial to resolve, but still annoying)
  • since it updates AssemblyInfo.cs every time it runs and not just when the version number needs to change it will cause the assembly to be rebuilt completely every time which makes the build process slower.

Alternatively there is a task in the "MSBuild Community Task" that can be used to generate a VersionInfo.cs file - just as good as AssemblyInfo but because this only contains the version it does not need to be checked into source control alleviating the 2nd issue above (at this point you comment out the AssemblyVersion attribute from the AssemblyInfo.cs file).

Another one from MSBuild Community Task is "SvnVersion" - it can be used to retrieve the Subversion revision number (there are other, equivalent tasks for other version control system). Using this is just as good if not better than the date so we avoid the 1st issue (though this will come back when my SVN number gets above 65536! But I have a long way to go for that).

There was still one issue which is the file would always be generated thus preventing the incremental build from shortening the build process as it is supposed to (in C or Java we can somewhat get away with that since the objects are built on a file-by-file basis but in C# they are always built into assemblies).

This custom MSBuild task addresses the problem by generating the VersionInfo file only if it is unchanged from the previous version.

Works only for C# projects at this point although it should be trivial to adapt it to work with VB projects.

How to use it


Installation:
  • Install the DLL and the Target file to a known directory
  • Install the MSBuild Community Task - this is used for the SvnVersion task.
  • Optionally, edit the target file to customize the way it generates the revision number. For example instead of SvnVersion you can use Time to get the current date.
  • Optionally, run the following command to prevent the "Unsafe Project" warning dialog (replace with actual path to the target file):
reg add HKLM\Software\Microsoft\VisualStudio\9.0\MSBuild\SafeImports /v AssemblyVersion /t REG_SZ /d "E:\Projects\SSSWorld\msbuild\AssemblyVersion.target"
(note that the key name is HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\MSBuild\SafeImports instead on Windows 64bit)

Usage for each project:
  • Remove AssemblyVersion and AssemblyFileVersion attribute from AssemblyInfo.cs
  • Add a VersionInfo.cs under the Properties folder of the project (does not have to contain anything as it will be overwritten automatically - and make sure it is NOT checked into source control)
  • Manually edit the project file and add the import line... Make sure the DLL is in the same directoy as the target file.

For more detail see the original blog post.
Last edited Aug 8 at 4:36 PM  by NicGaller, version 9
Updating...
Page view tracker