Determine which .NET Core runtime is needed

2019-02-09 02:21发布

问题:

I found some interesting articles about the difficulties of .NET Core SDK/runtime/tooling versioning, for example:

  • .NET Core Versioning
  • .NET Core versioning
  • Supporting both LTS and Current releases for ASP.NET Core

However I still don't really know how to deal with all of this in practice:

  1. Given a project with a number of .Net Core dependencies. How can I determine which version of the runtime needs to be available on the end-users machine?
  2. Does the runtime version need to match exactly, or can the runtime installed on the end-users machine be newer than the required version?
  3. Let's say I want to stick to some LTS version of the runtime. How can I determine the version of the packages I need to reference? How can I make sure that no newer packages are referenced?

Oh, and there is one more:

  1. Once I know which runtime version is required on the end-users machine, how can I determine (programmatically) if that version of the runtime (or a newer, backwards compatible one) is available?

回答1:

First, let's look at what happens when a portable .NET Core application is run via dotnet yourapp.dll:

  • The muxer executable (dotnet.exeon windows) loads a version of the host framework resolver (hostfxr) from a folder beneath host\fxr\ next to dotnet.exe.
  • The host framework resolver looks at yourapp.runtimeconfig.json (or a different file if configured when using dotnet exec) to find out which framework you are targeting. This extracts a framework name (Microsoft.NETCore.App) and a version (e.g. 1.1.0).
  • It then looks inside the shared\Microsoft.NETCore.App folder (based on the framework name) for available versions.
  • Based on the available versions and the framework version from yourapp.runtimeconfig.json it determines which version to use. Or it may decide to error out and complain that the required version is not available.