.NET Core App - 如何在运行时获取内部版本号 [英] .NET Core App - How to get build number at runtime

查看:92
本文介绍了.NET Core App - 如何在运行时获取内部版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Core MVC 应用程序,它是使用在线 TFS 构建的,并使用在线 TFS 中的发布管理内容发布到 Azure.一切都很好.

我想做的是让我的应用程序在某处显示内部版本号.在哪里无关紧要...但例如在像/api/buildversion 这样的 WebAPI 端点中.

谁能帮助我如何在运行时获取 TFS 内部版本号?当应用程序被打包/发布时,是否有任何文件包含我可以从应用程序访问的内部版本号?

解决方案

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

Appsettings.json 示例代码:

<代码>{连接字符串":{"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3;Trusted_Connection=True;MultipleActiveResultSets=true"},记录":{包括范围":假,日志级别":{"默认": "调试","系统": "信息",微软":信息"}}代码版本":{"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

    这篇关于.NET Core App - 如何在运行时获取内部版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆