In this tip, you will learn how to create a custom build and version number of .NET 5 and .NET 6, as found in the .NET Framework.
Introduction
.NET 5 and .NET 6 do not have default build and version number option, as found in the .NET Framework.
The version number is present in the AssemblyInfo.cs file in the Properties directory, and looks as follows:
[assembly: AssemblyVersion("1.0.*")]
This would replace the asterix with build and versions number; the result would look like 1.0.8306.30708, where the two last numbers are:
- build number, to be equal to the number of days since January 1, 2000 local time, and
- revision number to be equal to the number of seconds since midnight local time (without taking into account time zone adjustments for daylight saving time), divided by 2
This translates into the software being compiled on September 28th 2022, at 17:03.
A full description of the classic Build and Revision code in the assembly file can be found on Microsoft's page.
Creating Automatic Build and Version Number in .NET Framework 5.
The trick is to use an MSBuild Inline task to set the build and version number. More information can be found on Micosofts' page.
The following steps are needed:
- In order to add the
AssemblyVersion
, it must be added to the PropertyGroup
. - The event
BeforeBuild
must be set up. - The
UsingTask
must be created with standard C# code.
The code looks as follows:
<PropertyGroup>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Target Name="SetAssemblyVersion" BeforeTargets="BeforeBuild">
<SetBuildNumber>
<Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion" />
</SetBuildNumber>
</Target>
<UsingTask TaskName="SetBuildNumber" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyVersion ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage(MessageImportance.High, "Setting version number");
var now = DateTime.Now;
var secondsSinceMidnightDivivedBy2 = (int)(now - now.Date).TotalSeconds / 2;
var daysSinceJan1st2000 = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
AssemblyVersion = "1.2." + daysSinceJan1st2000.ToString() + "." +
secondsSinceMidnightDivivedBy2.ToString();
Log.LogMessage(MessageImportance.High, "Version number is: " + AssemblyVersion);
]]>
</Code>
</Task>
</UsingTask>
Options
This example will create a classic version number, but the option of using C# code to create the build and version, actually the entire assembly version number, gives the option to create any format wanted.
Issues
While this works for compiling and running software, it does not work with compiled software, where the application is packed into one file with all dependencies included.
The following example will fail:
dotnet publish $(SolutionDir)MyProject\MyProject.csproj
-p:PublishProfile=FolderProfile -r:win10-x64 -p:PublishSingleFile=true
-p:PublishReadyToRun=false -p:PublishTrimmed=false -c:Release -o:$(TargetDir)published
It will cause the following errors:
The task factory "CodeTaskFactory" is not supported on the .NET Core version of MSBuild.
and:
The task factory "CodeTaskFactory" could not be loaded from the assembly "C:\Program Files\dotnet\sdk\6.0.400\Microsoft.Build.Tasks.Core.dll". The task factory must return a value for the "TaskType" property.
As those issues are not critical to me, I have not investigated them further.
History
- 4th October, 2022: Initial version