Is there a way to make .NET 6 applications to run on specific .NET 6 runtime version

121 Views Asked by At

I have .NET6 application and I want to run this application on a specific .NET6 runtime version.

Let's say I have 2 .NET6 runtimes installed on my machine, i.e. 6.0.21 and 6.0.26. I want my .NET6 application to run on 6.0.21 runtime NOT on 6.0.26. It is always taking the latest 6.0.26 runtime only. How do I achieve this ?

I tried below 2 options to achieve this but didn't help,

  • By setting <RuntimeFrameworkVersion>6.0.21</RuntimeFrameworkVersion> in the csproj file
  • By adding global.json file with below entries
{
  "runtime": {
    "version": "6.0.21"
  }
}

Please note that to identify the runtime used by application, I added below line of code in the application which tells what is the runtime used. Currently it is displaying 6.0.26.

RuntimeInformation.FrameworkDescription

Kindly let me know if there is any other way to achieve this?

2

There are 2 best solutions below

0
shingo On BEST ANSWER
  1. Use .runtimeconfig.json, note that rollForward must be set to Disable

    "runtimeOptions": {
        "framework": {
          "name": "Microsoft.NETCore.App",
          "version": "6.0.21"
        },
        "rollForward": "Disable"
    }
    
  2. Use command line option --fx-version

    dotnet --fx-version 6.0.21 <YOUR DLL>
    
0
Shahar Shokrani On

You can deploy your app as a self-contained which means the the runtime selection occurs at the publish time (not run-time) read about here.

<PropertyGroup>
  <RuntimeFrameworkVersion>5.0.7</RuntimeFrameworkVersion>
</PropertyGroup>

Or via the cli:

dotnet publish --self-contained true /p:RuntimeFrameworkVersion=6.0.21