且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NET Core应用程序-如何在运行时获取内部版本号

更新时间:2023-11-08 18:44:46

简单的方法是,您可以将内部版本号存储在文件中(例如appsettings.json),然后在应用程序代码中获取此数据。



Appsettings.json示例代码:

  {
ConnectionStrings:{
DefaultConnection: Server =(localdb)\\ \mssqllocaldb;数据库= aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3; Trusted_Connection = True; MultipleActiveResultSets = true
},
记录:{
IncludeScopes: false,
LogLevel:{
默认:调试,
系统:信息,
微软:信息
}
}
CodeVersion:{
Num:#{MyBuildNumber}#
}
}




  1. 安装

    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?

    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