3

How can one read the project.json version number at runtime? I.e. the "1.0.0-1234" in the config below:

{
  "title": "MyProject.Api",
  "webroot": "wwwroot",
  "version": "1.0.0-1234",
  "dependencies": {
     ...
  },
  ...
}
Dave New
  • 38,496
  • 59
  • 215
  • 394
  • 1
    `Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion` will get your the full `version` value. – haim770 Jun 21 '16 at 11:23
  • @haim770 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:46

1 Answers1

8

You can read it using the static helper:

Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion 
Sock
  • 5,323
  • 1
  • 21
  • 32
  • I only get back "1.0.0.0" for the above example. Any idea why this would be the case? – Dave New Jun 21 '16 at 09:22
  • @davenewza check if you have version twice in my project.json – tmg Jun 21 '16 at 09:55
  • `ApplicationVersion` does not include any of the version after the `-`. so your version number is being truncated to `1.0.0`, which is equivalent to `1.0.0.0`. If you update your version to e.g. `1.0.0.1234`, or `1.1.0-1234`, you will see the version number as `1.0.0.1234` or `1.1.0.0` respectively – Sock Jun 21 '16 at 13:38
  • 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:46
  • The assembly version has historically ben ... (Major.Minor.Hotfix.Revision) and VS projects have always supported 1.0.0.* and the wildcard was replaced with an integer. Why asp.net core version decided to support a string and only after a hypen is crazy. Under the covers, it appears that the value is being cast to an integer and that is why the hyphen and everything after is lost. – evermeire Sep 26 '16 at 22:34
  • 1
    "Why asp.net core version decided to support a string" - to conform with the SemVer standards. http://semver.org/ – Sly Gryphon Feb 22 '17 at 02:44