Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

Getting the .NET Core Runtime Version in a Running Application


On this page:

One thing I like to do as part of my applications that are running is to have an information page that gives some basic information about the runtime environment the application is running under.

For example, here's what I add to my info page in my AlbumViewer Sample:

I find it useful especially these days with SDKs and runtimes switching so frequently that I can quickly determine what versions the application actually runs under and where it's hosted (Window/Linux/container) without having to actually look at the source code.

This should be easy right? Well, it may not be difficult, but obvious it is not.

Getting the Runtime Version

You would think it would be easy to get runtime version information - after all the runtime is... well... it's running. But nooooo... Microsoft has never made it easy to get proper runtime version information that's suitable for display in an application. Hell, in full framework you had to resort to checking the registry and then translating magic partial version numbers to an official release version number (like 4.7.1). You know you're doing it wrong when you can't tell what version of a runtime you have installed without looking in the registry and looking at an obscure lookup table to resolve the actual version everyone expects to look at.

This trend continues in .NET Core. There's no direct API that returns a version number like 1.1, 2.0 or 2.1 etc. Because why make the obvious easy?

There are lots of APIs that you might think would work but they either don't return anything or return some unrelated version number like the system installed full framework version - but not the one of the actual running runtime.

I'm not going to bore with all the false starts I've had here. If you want to find the .NET Core Runtime that your application is targeting you can look at the TargetFrameworkAttribute in your application's startup assembly:

var framework = Assembly
    .GetEntryAssembly()?
    .GetCustomAttribute<TargetFrameworkAttribute>()?
    .FrameworkName;

var stats = new
{                
    OsPlatform = System.Runtime.InteropServices.RuntimeInformation.OSDescription,
    AspDotnetVersion = framework
};

It seems pretty hacky but it should be fairly reliable since every application has to have a a TargetFramework associated with it. This value comes from the project file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
</project>

and the project build process turns that into an attribute attached to the startup assembly.

It's a shame that this isn't exposed somewhat more logically like the property that actually exists but is not set:

string runtime = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;

Sadly that value very usefully returns null.

Use the force wisely, Luke!

OK, so now you can get the version number, but you should be wary of using it for anything other than displaying it for reference information. Version numbers are not really meant to be used for feature detection or other application critical comparisons, so use this version number only in cases where you absolutely have no other way. I suspect this is one of the reasons the .NET teams refuse to build runtime version numbers directly into the frameworks.

Personally, I tend to use compiler directives to differentiate broad feature 'versions' - I've talked about this a bit in my previously posts on building components that target both full framework and .NET Standard. This works great for broad frameworks, which in most cases is all you need to differentiate for, but can also be used for more specific targets.

Summary

Version detection in .NET Runtimes is not obvious for most versions and there's no direct built-in way to get a friendly description of the version you're running.

So, the above hack is it - it works. For now 😃

this post created and published with Markdown Monster
Posted in .NET Core  ASP.NET Core  

The Voices of Reason


 

Bruce
April 13, 2018

# re: Getting the .NET Core Runtime Version in a Running Application

With Minor-Version Roll-forward coming in Core 2.1, the target framework you built the app for may not be the one it eventually finds itself running on.


MV10
April 23, 2018

# re: Getting the .NET Core Runtime Version in a Running Application

In addition to what Bruce mentions, there is an open issue on one of the .NET Core github repos about this problem. (Target is also a tricky question because different parts of the app could have different-but-compatible targets.)


Rick Strahl
April 23, 2018

# re: Getting the .NET Core Runtime Version in a Running Application

@Bruce, MV10 - exactly my point. The runtime should provide this info so we don't have to spelunk around and hack at this. There should be a way to know what the actually running runtime instance is.

Given what we have though I think this at least gives us a way to differentiate versions and what it's meant to run under. Not perfect - agreed, but... 😃


Bruno Garcia
May 15, 2018

# re: Getting the .NET Core Runtime Version in a Running Application

A few days ago while trying to capture this information to report to Sentry.io I started a library called Sentry.PlatformAbstractions on GitHub. That is a work in progress but there's some functionality running already >current SDK version.

So far, regarding Runtime, it can report: .NET Framework 1.0 to 4.7.2 and .NET Core (if not self contained) and Mono. There are screenshots (at the bottom) of this blog post.

The goal is to add better more OS information (report not only Linux and kernel but which distro and for Windows, if it's Server edition or not).

Everything is open source and help would be very much appreciated!


Jan
July 11, 2018

# re: Getting the .NET Core Runtime Version in a Running Application

I'm using

"Version: " + System.Environment.Version +
" from: "+ System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

to figure out the .net version. It prints

Version: 4.0.30319.42000 from: /usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.2/

on Ubuntu and

Version: 4.0.30319.42000 from: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.2\

on Win7

 

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2018