0

I want to display a version on my Blazor app that I set in my Azure DevOps pipeline. I'm using the VersionDotNetCoreAssemblies task in DevOps:

- task: VersionDotNetCoreAssemblies@2
 inputs:
    Path: 'src/Claims.Web'
    VersionNumber: '$(Build.BuildNumber)'
    Injectversion: True
    FilenamePattern: '.csproj'
    Field: 'Version'
    OutputVersion: 'OutputedVersion'
    AddDefault: true

which logs the following:

Source Directory:  src/Claims.Web
Filename Pattern: .csproj
Version Number/Build Number: 2022-01-25.10-Claims
Version Filter to extract build number: \d+\.\d+\.\d+\.\d+
Field to update (all if empty): Version
Add default field (all if empty): true
Output: Version Number Parameter Name: OutputedVersion
Inject Version: true
SDK names: Microsoft.NET.Sdk
Using provided version number directly
Extracted Version: 2022-01-25.10-Claims
Matched the file 'Claims.Web.csproj' using the SDK name 'Microsoft.NET.Sdk'
Adding file Claims.Web.csproj as is a .NETCore Project
Will apply 2022-01-25.10-Claims to 1 files.
Getting just the PropertyGroup that contains the single version fields
Found <PropertyGroup> [1] blocks
The <Version> version is not present in the file so adding it
The src\Claims.Web\Claims.Web.csproj file only targets 1 framework
src\Claims.Web\Claims.Web.csproj - version applied
Set the output variable 'OutputedVersion' with the value 2022-01-25.10-Claims
(node:1484) Warning: Use Cipheriv for counter mode of aes-256-ctr

2022-01-25.10-Claims is the version I want to display on the web. So, at the moment I'm using the following on my page:

@GetType()?.Assembly?.GetName()?.Version?.ToString()

However, that displays 2022.0.0.0

Currently, my csproj does not contain a <Version /> property, but I don't believe that is required from what I can tell. My understanding is the VersionDotNetCoreAssemblies task should add it if necessary.

If relevant, after my VersionDotNetCoreAssemblies task, I run a dotnet restore and a dotnet publish.

What do I need to modify to display the Version Number/Build Number of 2022-01-25.10-Claims?

Ryan Buening
  • 1,559
  • 2
  • 22
  • 60
  • 1
    You can't use an arbitrary version string. It must follow specific formatting rules - 4 numbers separated by dots and a suffix that's typically used to denote preview versions. If you want to include strings like `Claims` you probably aren't looking for an assembly or build version – Panagiotis Kanavos Jan 25 '22 at 23:32
  • 1
    There are other assembly attributes you can use, eg [InformationalVersion](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assemblyinformationalversionattribute?view=net-6.0), `AssemblyProduct`, `AssemblyTitle` or even [AssemblyMetadata](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assemblymetadataattribute?view=net-6.0) which allows you to define arbitrary key/value pairs. If `Claims` is a project/feature/product name, `AssemblyProduct` is probably the best option – Panagiotis Kanavos Jan 25 '22 at 23:36
  • 1
    The assembly version is used for version resolution. If your project references a specific assembly, it will refuse to use an assembly with the same name but different version. You *don't* want some information text change to break compilation. – Panagiotis Kanavos Jan 25 '22 at 23:38
  • @PanagiotisKanavos - I updated my build number to `20220126.3.0.0` and the `VersionDotNetCoreAssemblies` task logs: `Version Number/Build Number: 20220126.3.0.0 Version Filter to extract build number: \d+\.\d+\.\d+\.\d+ ... Will apply 20220126.3.0.0 to 1 files`. However, during dotnet publish I receive the following error: `error CS7035: The specified version string does not conform to the recommended format - major.minor.build.revision` – Ryan Buening Jan 26 '22 at 14:26

1 Answers1

1

The AssemblyName.Version property must be in the format 0.0.0.0 so that is why your custom Build number cannot be converted to an assembly version by the VersionDotNetCoreAssemblies task. It cannot convert the dashes and alphanumeric characters into the numeric-only format.

You could change to a build number that fits into 0.0.0.0

Note that 20220126.3.0.0 is not valid as a version number as 20220126 is too large. see this answer. 2022.1.26.3 would be valid. You could use other attributes to store your version as mentioned in the comments.

or take the Build number from the DevOps pipeline and set it where you app can access it. This depends on your hosting. If you are using Docker you can set a variable inside the container or for App service you can set an application setting with the build number.

In DevOps the build number is in an environment variable called "Build_BuildNumber"

Rosco
  • 2,108
  • 17
  • 17
  • It's possible to have a suffix after the build number `20220125.10.0-Claims` would work – Panagiotis Kanavos Jan 25 '22 at 23:30
  • Thanks for the reply. I updated my build number to `20220126.3.0.0` and the `VersionDotNetCoreAssemblies` task logs: `Version Number/Build Number: 20220126.3.0.0 Version Filter to extract build number: \d+\.\d+\.\d+\.\d+ ... Will apply 20220126.3.0.0 to 1 files.` However, during dotnet publish I receive the following error: `error CS7035: The specified version string does not conform to the recommended format - major.minor.build.revision` – Ryan Buening Jan 26 '22 at 13:17
  • 1
    There are restrictions on version numbers. see https://stackoverflow.com/questions/37941195/the-specified-version-string-does-not-conform-to-the-required-format-major-mi – Rosco Jan 27 '22 at 09:19
  • Thanks for the info. I was able to get this working with your updated answer – Ryan Buening Jan 27 '22 at 13:50