Question

Could not load file or assembly 'System.Runtime, Version=7.0.0.0...' - After installing .NET Core 7 'dotnet watch run' not working

After the .Net 7.0 update, when I use dotnet watch run I get this error:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. File name: 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at System.Reflection.RuntimeAssembly.GetType(QCallAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive, ObjectHandleOnStack assemblyLoadContext) at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at System.Reflection.Assembly.GetType(String name, Boolean throwOnError) at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook) at System.StartupHookProvider.ProcessStartupHooks()

I can successfully build and run the project using Visual Studio, but can't use dotnet cli. How can this error be fixed?

 46  45536  46
1 Jan 1970

Solution

 45

According to the Microsoft document, the SDK uses the latest installed version and this is an expected behavior:

The .NET CLI must choose an SDK version for every dotnet command. It uses the latest SDK installed on the machine by default, even if the project targets an earlier version of the .NET runtime.

So, it is not a real solution but changing the target framework of the project solves the issue.

enter image description here


Changing .Net version of the project is not a simple process so as a workaround, you can add a dummy word to the end of CLI command and it works:

dotnet watch run xyz

Another workaround, global.json below may help: select the .NET version to use

On rare occasions, you may need to use an earlier version of the SDK. You specify that version in a global.json file. The "use latest" policy means you only use global.json to specify a .NET SDK version earlier than the latest installed version.

Create a global.json file and point it to the v6 SDK. global.json can be placed anywhere in the project file hierarchy.

{
    "sdk": {
        "version": "6.0.403"
    }
}

You can find out current and installed SDK's by running:

dotnet --info
2022-11-08

Solution

 45

According to the Microsoft document, the SDK uses the latest installed version and this is an expected behavior:

The .NET CLI must choose an SDK version for every dotnet command. It uses the latest SDK installed on the machine by default, even if the project targets an earlier version of the .NET runtime.

So, it is not a real solution but changing the target framework of the project solves the issue.

enter image description here


Changing .Net version of the project is not a simple process so as a workaround, you can add a dummy word to the end of CLI command and it works:

dotnet watch run xyz

Another workaround, global.json below may help: select the .NET version to use

On rare occasions, you may need to use an earlier version of the SDK. You specify that version in a global.json file. The "use latest" policy means you only use global.json to specify a .NET SDK version earlier than the latest installed version.

Create a global.json file and point it to the v6 SDK. global.json can be placed anywhere in the project file hierarchy.

{
    "sdk": {
        "version": "6.0.403"
    }
}

You can find out current and installed SDK's by running:

dotnet --info
2022-11-08

Solution

 13

Giving all more context here. After the dotnet update to 7.0 version, I started getting the runtime issue using cli commands. Specifically dotnet watch inside a project from a .sln solution.

This behavior is expected, as per the .NET SDK DOC here. Ideally, I believe a global.json file, pointing to expected SDK version, should be created from the beginning, but it's not.

If you go over Tools > Preferences > SDK Locations > .NET Core (on VS) you will be able to see all the available versions ordered by the latest version. All we have to do here to fix this is create a global.jsonfile inside the project providing the same .net version defined in the project properties. This file looks like below:

{
  "sdk": {
    "version": "X.X.X"
  }
}

Observe that X.X.X should be replaced by the .NET version of the project.

In the end, the problem is caused by the hierarchy of .NET version search by the cli, it will always look for the latest except in the case you define this global.json file.

2022-11-10

Solution

 13

Giving all more context here. After the dotnet update to 7.0 version, I started getting the runtime issue using cli commands. Specifically dotnet watch inside a project from a .sln solution.

This behavior is expected, as per the .NET SDK DOC here. Ideally, I believe a global.json file, pointing to expected SDK version, should be created from the beginning, but it's not.

If you go over Tools > Preferences > SDK Locations > .NET Core (on VS) you will be able to see all the available versions ordered by the latest version. All we have to do here to fix this is create a global.jsonfile inside the project providing the same .net version defined in the project properties. This file looks like below:

{
  "sdk": {
    "version": "X.X.X"
  }
}

Observe that X.X.X should be replaced by the .NET version of the project.

In the end, the problem is caused by the hierarchy of .NET version search by the cli, it will always look for the latest except in the case you define this global.json file.

2022-11-10