22

How do I display application version from the project.json? I am using gulp-bump to autoincrement version, but I can't show the recent version. Here is what I'm trying:

@(Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion)

This does not work, it displays "1.0.0" instead of real value from project.json

I also tried this but it looks like it is no longer works in RC2:

@inject Microsoft.Extensions.PlatformAbstractions.IApplicationEnvironment appEnv
My version number is @(appEnv.ApplicationVersion)
vmg
  • 9,920
  • 13
  • 61
  • 90

5 Answers5

23

Since Platform Abstractions were obly shipped with ASP.NET Core 1 and has been removed from ASP.NET Core 2 and up, if you're using version 2 or above, you must replace this row:

Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion

with this one:

System.Reflection.Assembly.GetEntryAssembly().GetName().Version

as specified in "Replacing API usage" section of the previous linked page.

Luca
  • 1,588
  • 2
  • 22
  • 26
21

As per this announcement, IApplicationEnvironment no longer exists.

You can still access the ApplicationVersion statically using:

Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion

It works for me. My project.json looks like this:

{
    "version": "1.0.0.2",
    // all the rest
}

And in my index view, I have the following line at the top:

@Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion

And I correctly get 1.0.0.2 in the output. And when I change that value and restart (build) the application, the new version is shown there.

poke
  • 369,085
  • 72
  • 557
  • 602
  • 3
    sorry, my question was not clear enoguh: I am already using that, but it displays 1.0.0.0 instead of expected 1.0.0.2 – vmg Jun 13 '16 at 20:07
  • 1
    thanks! how do you bump project version (automatically)? – vmg Jun 13 '16 at 20:23
  • I never used `gulp-bump` so no idea how that works. It should be enough to just modify the version in `project.json` and the rebuild the app. – poke Jun 13 '16 at 20:26
  • thanks. gulp-bump does not support 4-number versions strings :( that is the reason – vmg Jun 13 '16 at 20:27
  • 1
    Oh, I see, that’s a shame! Although I guess four digit versions are not valid according to [semver](http://semver.org/), so you should probably (maybe?) avoid them in .NET Core projects (since it aims to follow semver). – poke Jun 13 '16 at 20:28
  • Hah, I’m glad you figured it out :) – poke Jun 13 '16 at 21:02
  • I added `.TrimEnd('0','.')` to remove trailing '.0's. As long as my third set is nonzero, I get the 3 places without the extra trailing .0. Wish it was smart enough to leave it off when you're using 3 number strings. – dsghi Jul 03 '16 at 22:21
  • 1
    This doesn't work in dotnet 1.0.0. Gives you "File version" which `dotnet publish --version-suffix xxx` doesn't change. What it actually does change is "Product version" but I have no idea how to get this one. – rook Sep 22 '16 at 22:48
  • Fair warning: This gives you the version of the *executing application*, which is not necessarily the version number of the current assembly (assuming the current assembly is a DLL). When I ran this, I got the version of NUnit that I ran the test under... – NightOwl888 Apr 11 '17 at 12:04
14

I used a different approach, as stated in this answer which gave me a SemVer version (1.0.0) which is actually in my project.json and not 1.0.0.0, which is returned by accepted answer. So the code would be:

var runtimeVersion = typeof(Startup)
            .GetTypeInfo()
            .Assembly
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
            .InformationalVersion;

It returns correct suffixed versions as well, i.e. something like "2.0.1-dev01"

Community
  • 1
  • 1
Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49
11

This worked for me for .NET Core 2.0.5.

Code:

var assemblyVersion = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute<System.Reflection..AssemblyInformationalVersionAttribute>().InformationalVersion;
Console.WriteLine(assemblyVersion);

myproject.csproj

<PropertyGroup>
    <VersionPrefix>1.0.0.1</VersionPrefix>
    <VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>

outputs

1.0.0.1-alpha
sabbour
  • 4,969
  • 6
  • 34
  • 33
3

If you end up here and you are trying to manage project versions through the solution version.

Adding a Directory.Build.props file to the solution directory containing:

<Project>
  <PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
  </PropertyGroup>
</Project>

Will set the Version property of all included projects that 'Get version from parent solution'.

Not ideal, but it allows you to manage the version in a single place for a version locked multi-project solution.

saarrrr
  • 2,754
  • 1
  • 16
  • 26