Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core App - How to get build number at runtime

I've got a .NET Core MVC app which is built using TFS online, and published to Azure using the Release management stuff in TFS online. All very nice.

What I'd like to do is have my app display the build number somewhere. Doesn't matter where...but for example in a WebAPI endpoint like /api/buildversion.

Can anyone help with how I can get the TFS build number at runtime? When the app is packaged/published is there any file which contains the build number that I can access from the application?

like image 753
Paul Avatar asked Feb 21 '17 22:02

Paul


People also ask

How do I check the version of .NET Core in an application?

You can see both the SDK versions and runtime versions with the command dotnet --info .

How do I find my build number in Azure DevOps?

Build and Release variables can be found in the Microsoft Documentation, but the more useful ones include: $(Build. BuildNumber) - This is the full build number (see above for setting an appropriate format). $(Build.

What is .NET Core in C#?

NET Core is used to create server applications that run on Windows, Linux and Mac. It does not currently support creating desktop applications with a user interface. Developers can write applications and libraries in VB.NET, C# and F# in both runtimes.

Does dotnet run also build?

Builds and runs the app using the specified framework. The framework must be specified in the project file. Forces all dependencies to be resolved even if the last restore was successful.


2 Answers

The simple way is that you can store the build number in a file (e.g. appsettings.json), then get this data in app code.

Appsettings.json sample code:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
  "CodeVersion": {
    "Num": "#{MyBuildNumber}#"
  }
}
  1. Install Replace Tokens extension
  2. Edit your build definition
  3. Click Variables tab and add a variable. (Name: MyBuildNumber, Value:$(Build.BuildNumber))
  4. Add Replace Tokens build step before build step

enter image description here

like image 132
starian chen-MSFT Avatar answered Sep 20 '22 11:09

starian chen-MSFT


There is an easier way than adding more plugins and libraries. If you are using the web/app deploy method, expand the File Transforms & Variables enter image description here

Assuming you want to update say a build number value in your appsettings.json

enter image description here

Then just update the variables setting in your release definition of VSTS enter image description here

Super easy. This is more or less the same steps in Octopus IIRC.

like image 25
macm Avatar answered Sep 22 '22 11:09

macm