Reputation: 10244
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?
Upvotes: 47
Views: 56078
Reputation: 10244
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.
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
Upvotes: 47
Reputation: 171
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.json
file 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.
Upvotes: 13
Reputation: 91
For me, the solution was to add a global.json to the root of your project. With the SDK version, you want to use.
{ "sdk": { "version": "6.0.403" } }
For me 6.0.403 that forces dotnet watch to use this version.
Upvotes: 5