How display application version in ASP.NET Core

Posted by Anuraj on Sunday, November 25, 2018 Reading time :1 minute

ASP.NET Core

Most enterprise application soon or latter, have a requirement to trace out the version of a currently running application. This post shows how to display application version in a web app and how to increment it as part of build process.

You can get the version of your app in ASP.NET Core 2.x like this - Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion

I implemented it as a service and the service will return a string property version, which can be injected to the views.

public interface IAppVersionService
{
    string Version { get; }
}

And here is the implementation.

public class AppVersionService : IAppVersionService
{
    public string Version => 
        Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
}

Next you can inject the service to the controllers and views.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddTransient<IAppVersionService, AppVersionService>();
}

And here is the code in the view - _ViewImports.cshtml

@using HelloMVC
@using HelloMVC.Models
@using HelloMVC.Services
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@inject IAppVersionService AppVersionService
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

And you can use it in the view like this.

<footer class="border-top footer text-muted">
    <div class="container">
        Version : @AppVersionService.Version
    </div>
</footer>

Which will displayed like this.

App version display

By default it will display version 1.0.0.0, if you want to modify it you can add version element with required version info like this.

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>
  <Version>1.2.3.4</Version>
</PropertyGroup>

One of the common requirement is updating the version number while deploying to QA / Production environments, to do this you can use some CI configuration variables. For Azure DevOps, you can use $(BUILD_BUILDNUMBER) variable. And you can configure it from Options > Build Properties.

Azure DevOps - Build Options

And you need to modify your project code like this.

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>
  <Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">1.2.3.4-Dev</Version>
  <Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
</PropertyGroup>

If the $(BUILD_BUILDNUMBER) variable is available, then it will take that otherwise (local dev environment) it will display like 1.2.3.4-Dev. The $(BUILD_BUILDNUMBER) will be populated like Year,Month,Day of Month and Build number.

Happy Programming :)


Similar Posts


Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



Translate
Subscribe
BMC logoBuy me a coffee


Copyright © 2018 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub