26

None of what used to work in RC.x helps anymore.

I have tried these:

  1. PlatformServices.Default.Application.ApplicationVersion;

  2. typeof(Controller).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

  3. Assembly.GetEntryAssembly().GetName().Version.ToString();

They all return 1.0.0.0 instead of 1.0.0-9 which should be after execution of the dotnet publish --version-suffix 9 having this in project.json: "version": "1.0.0-*"

Basically they give me "File version" from the attached picture instead of "Product version" which dotnet publish actually seems to change.

enter image description here

Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
rook
  • 2,819
  • 4
  • 25
  • 41

5 Answers5

32

For version 1.x:

Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

For version 2.0.0 this attribute contains something ugly: 2.0.0 built by: dlab-DDVSOWINAGE041 so use this one:

typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
rook
  • 2,819
  • 4
  • 25
  • 41
  • Thanks, that is very helpful. Why asp.net core team didn't support 1.0.0.* instead of 1-* is beyond me. The version numbers for Microsoft .NET assemblies have always been int.int.int.int and makes sense from a programmatic standpoint. Supporting a string in a build # is not necessary and leads to other issues. – evermeire Sep 26 '16 at 22:37
  • Instead, use System.Reflection.IntrospectionExtensions.GetTypeInfo( typeof(ArmoredOutputStream) ) .Assembly.GetCustomAttribute().Version; // Note: where ArmoredOutputStream is a class in the assembly whose version you want – Stefan Steiger Apr 11 '17 at 19:40
  • The edit is misleading and probably due to some customized settings from the editor. The correct answer is Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion FileVersion drops the prerelease tags – Cyprien Autexier Dec 14 '17 at 01:16
  • In addition be mindful about GetEntryAssembly as it might not be what you expect. I some cases you might prefer GetExecutingAssembly() or getting it from some known type. Using typeof(RuntimeEnvironment).GetTypeInfo().Assembly will of course give you some system assembly version number... – Cyprien Autexier Dec 14 '17 at 01:26
  • @evermeire re: `int.int.int.int` The AssemblyVersion and AssemblyFileVersion are defined to be just 4-ints, but the AssemblyInformationalVersion is defined as a string and may have the `-` and words etc. – Jesse Chisholm May 24 '18 at 19:36
  • 1
    for .net core 2.2, `Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion;` seem to be returning the correct Product version – Abhinav Galodha Nov 29 '18 at 20:32
  • @Abhinav Galodha Looks like you have the correct answer! – zezba9000 Feb 11 '19 at 05:16
26

I would do it like this on ASP.NET Core 2.0+

var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();
CREM
  • 1,929
  • 1
  • 25
  • 35
  • 1
    Although lowest rated, this one is working for me, configured version under project properties => Packages => Assembly (file) version. Returns all 4 version parts. – CularBytes May 01 '18 at 08:19
  • 1
    This is also the solution that worked for me. Seems the RuntimeEnvironment in .Net Core 2 MVC application does not contain the right functions, but Startup do. – nivs1978 Nov 20 '18 at 10:13
  • 1
    Silly question, but is this 100% guaranteed to work for other referenced libraries outside of the executing one? I'm porting a .NET framework 4.6-7 app which is currently looking for file locations of each dll in order to get the file versions.. I just wanted to make sure this is an equivalent replacement as long as I choose a type that is only located in that assembly. – Dinerdo Mar 28 '19 at 01:03
6

In .Net Core 3.1 I show the version directly in my View using:

         @GetType().Assembly.GetName().Version.ToString()

This shows the Assembly Version you have in your csproj file:

<PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <AssemblyVersion>4.0.0.0</AssemblyVersion>
  <FileVersion>2.2.2.2</FileVersion>
  <Version>4.0.0-NetCoreRC</Version>
</PropertyGroup>

If you want to display the "other" FileVersion or "Informational" Version properties in the View add using System.Reflection:

using System.Reflection;
.... bunch of html and stuff
<footer class="main-footer">
        <div class="float-right hidden-xs">
            <b>Assembly Version</b> @(Assembly.GetEntryAssembly().GetName().Version)
            <b>File Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version)
            <b>Info Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion)
        </div>    
    </footer>

Note that after adding the System.Reflection the original @GetType().Assembly.GetName().Version.ToString() line returns 0.0.0.0 and you need to use the @Assembly.GetEntryAssembly().GetName().Version

There's a blog post here

Edit: Make sure to follow proper naming conventions for the Version strings. In general, they need to lead with a number. If you don't, your app will build but when you try to use NuGet to add or restore packages you'll get an error like 'anythingGoesVersion' is not a valid version string. Or a more cryptic error: Missing required property 'Name'. Input files: C:\Users....csproj.' more here:

Michael G
  • 535
  • 4
  • 12
  • Actually this seems to be the correct working one. It shows the version number I set (2.0.1 for me) in the Package tab. This was what I was looking for and it is just fine. The accepted answer shows crap : "4.700.21.11602" – Venugopal M Nov 07 '22 at 05:54
4

This work for me too:

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

It works with csproj file - either <Version>1.2.3.4, or <VersionPrefix>1.2.3</VersionPrefix>. However the <VersionSuffix> isn't recoganized as this doc says.

Calvin
  • 1,153
  • 2
  • 14
  • 25
0

The answer by Michael G should have been the accepted one since it works as expected. Just citing the answer by Michael G above.

var version = GetType().Assembly.GetName().Version.ToString();

works fine. It gets the Package version set in the Package tab of project properties. As an addition, if we need to get the Description we set in the same tab, this code would work. (core 3.1)

string desc = GetType().Assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;

Just in case someone needs this. Happy coding !!!

Venugopal M
  • 2,280
  • 1
  • 20
  • 30