Whenever a new .NET assembly project is created in Visual Studio, a file named AssemblyInfo is created that contains attributes used to define the version of the assembly during compilation. Using assembly versions effectively enables various team members to identify deployed assemblies and helps troubleshoot problems that may occur in a particular environment (e.g. Development, Test, or Production).
When building the solution, there are two version numbers that need to be considered: the file version number and the .NET assembly version number. As part of the best practices, the AssemblyFileVersion attribute should be incremented automatically as part of the build process. It is therefore intended to uniquely identify a build. The AssemblyVersion attribute is the version that .NET uses when linking assemblies. In this blog post, we’ll learn how to use the build process to auto specify the AssemblyFileVersion and AssemblyInformationalVersion.
First, we need to create and check a custom written PowerShell script into our codebase:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Update-AssemblyInfoVersionFile | |
{ | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$productVersion | |
) | |
$buildNumber = $env:BUILD_BUILDNUMBER | |
if ($buildNumber -eq $null) | |
{ | |
$buildIncrementalNumber = 0 | |
} | |
else | |
{ | |
$splitted = $buildNumber.Split('.') | |
$buildIncrementalNumber = $splitted[$splitted.Length – 1] | |
} | |
$SrcPath = $env:BUILD_SOURCESDIRECTORY | |
Write-Verbose "Executing Update-AssemblyInfoVersionFiles in path $SrcPath for product version Version $productVersion" –Verbose | |
$AllVersionFiles = Get-ChildItem $SrcPath AssemblyInfo.cs –recurse | |
$versions = $productVersion.Split('.') | |
$major = $versions[0] | |
$minor = $versions[1] | |
$patch = $versions[2] | |
$assemblyVersion = $productVersion | |
$assemblyFileVersion = "$major.$minor.$patch.$buildIncrementalNumber" | |
$assemblyInformationalVersion = $productVersion | |
Write-Verbose "Transformed Assembly Version is $assemblyVersion" –Verbose | |
Write-Verbose "Transformed Assembly File Version is $assemblyFileVersion" –Verbose | |
Write-Verbose "Transformed Assembly Informational Version is $assemblyInformationalVersion" –Verbose | |
foreach ($file in $AllVersionFiles) | |
{ | |
(Get-Content $file.FullName) | | |
%{$_ -replace 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyVersion(""$assemblyVersion"")" } | | |
%{$_ -replace 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyFileVersion(""$assemblyFileVersion"")" } | | |
%{$_ -replace 'AssemblyInformationalVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyInformationalVersion(""$assemblyInformationalVersion"")" } | | |
Set-Content $file.FullName –Force | |
} | |
return $assemblyFileVersion | |
} | |
Update-AssemblyInfoVersionFile $args[0] |
Once its checked in, we need to modify the build definition to include a PowerShell task and provide the script path and arguments as shown below:

Do note that above task should come before you start building solution using msbuild or visual studio task. Also note that in the arguments, we have mentioned 1.0.0. It will be used to identify the AssemblyVersion attribute.
Now, we need to go ahead and start the build. During build we can see output like below for PowerShell task added:
2017-09-04T04:16:08.5335455Z VERBOSE: Executing Update-AssemblyInfoVersionFiles in path d:\a\1\s for product version Version 1.0.0 2017-09-04T04:16:08.8305461Z VERBOSE: Transformed Assembly Version is 1.0.0 2017-09-04T04:16:08.8305461Z VERBOSE: Transformed Assembly File Version is 1.0.0.9 2017-09-04T04:16:08.8315482Z VERBOSE: Transformed Assembly Informational Version is 1.0.0 2017-09-04T04:16:08.8905460Z 1.0.0.9 2017-09-04T04:16:08.9095447Z ##[section]Finishing: PowerShell Script
Now if we download artifacts and see the properties of assembly generated, we would be able to see the correct metadata.
For more information on various assembly versioning attributes, please refer this link on stackoverflow
Cool article! Is the powershell script picture missing in the article?
LikeLike
Forget it, I see it now. Thanks.
LikeLike
Thanks for your kind words. I’m very glad that you liked it.
LikeLike
when i copy the script in powershell in VSTS, i am getting error message as TF20507: The string argument contains a character that is not valid:’u0009′. Correct the argument, and then try the operation again.
Parameter name: definition.Steps.script
can you help
LikeLike
That’s a TAB character. You need to check for the same using line number mentioned in the error output. It is most likely to be hidden in one of the user input values.
LikeLike