Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET / .NET6

Classic or Custom Build and Version Numbers for Assembly Version for .NET 5, .NET 6

4.03/5 (6 votes)
4 Oct 2022CPOL2 min read 13.6K  
Classic/custom build for assembly version for .NET 5 and 6.
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:

C#
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")] 
// when commented out it uses the assembly version

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:

  1. build number, to be equal to the number of days since January 1, 2000 local time, and
  2. 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:

  1. In order to add the AssemblyVersion, it must be added to the PropertyGroup.
  2. The event BeforeBuild must be set up.
  3. The UsingTask must be created with standard C# code.

The code looks as follows:

C#
// add the property "AssemblyVersion" to the propertyGroup:
<PropertyGroup>
  <AssemblyVersion>1.0.0.0</AssemblyVersion>
  // more properties here.....

// add the pre-build event, named BeforeBuild in .NET Core:
<Target Name="SetAssemblyVersion" BeforeTargets="BeforeBuild">
  <SetBuildNumber>
    <Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion" />
  </SetBuildNumber>
</Target>
// and the task creating the AssemblyVersion
<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");
  	  
  	  // old style automatic build and 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)