Set the AssemblyFileVersion to match the build number

If you are not sure why you need to set the AssemblyFileVersion and AssemblyVersion read this first.

First thing to do is to change the build number format to include the version number of a build Major.Minor.Build.Revision.

Something like this $(BuildDefinitionName_{Major}.{Minor}.$(DayOfYear)$(Rev:.r) will usually do the trick. The Build number could also be a date $(Date:MMdd)

The above build number format will will result in a build number similar to: Calculator.Main.QA_5.3.321.1

For the Major and Minor numbers create two string arguments in your Build process one for each.

Find the Invoke For Reason activity named “Update Build Number for Triggered Builds” in your build process and add an Assign activity right before the Update Build Number activity. This will include your Major and Minor build numbers in the Build Number Format.

Assign activity
Value: String.Format(BuildNumberFormat, Major, Minor)

Now we want to put the version number from the build into the AssemblyInfo.cs file in our projects so the assemblies have the same version number as the build.

There are 3 build activities we can use to get the job done.

We will need the following variables defined:

  1. FileVersion as string
  2. FilesToVersion as IEnumerable<String>

Then add these four activities into a sequence right after initialize Workspace.

  1. 1. Extract the Version Number from the Build Number
  2. Assign activity (From the Primitives group in your Workflow tool box)
  3. To: FileVersion
  4. Value: BuildDetail.BuildNumber.Substring(BuildDetail.BuildNumber.IndexOf("_") + 1)

2. Get all the AssemblyInfo.cs files
FindMatchingFiles activity (from the Team Foundation Build Activities in your Workflow tool box)
Match Pattern: String.Format("{0}\**\Assemblyinfo.cs", SourcesDirectory)
Result: FilesToVersion

  1. 3. Change the AssemblyFileVersion
    File activity (from the TFS Build Extensions project)
  2. Action: Replace
  3. Fail Build on Error: True
  4. Files: FilesToVersion
  5. Regex Pattern: "AssemblyFileVersion\(""(.*)""\)"
  6. Replacement: String.Format("AssemblyFileVersion(""{0}"")", FileVersion)
4. Change the AssemblyVersion

File activity (from the TFS Build Extensions project)
Action: Replace
Fail Build on Error: True
Files: FilesToVersion
Regex Pattern: "AssemblyVersion\(""(.*)""\)"
Replacement: String.Format("AssemblyVersion(""{0}.{1}.0.0"")", Major,Minor)

When you queue a build set the Major and Minor numbers on the build definition and let the build process do the rest.

The result will be an AssemblyFileVersion that looks like 5.3.321.1 and an AssemblyVersion that looks like

The TFS Build Extensions has a TFSVersion Activity that is great and helps to simplify this. However it currently has a problem with a BuildNumberFormat that is different from the default. When that gets fixed i will repost how to do this using TFSVersion.