1

I have a project that is using net6 as the target framework. I recently installed .net core 7 SDK on my PC. After that, when I use dotnet watch run I get a strange 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.
.....

As the error suggests it is looking for .net 7 dlls in my project, while my project is targeting .net 6!

For a quick hack, I added a global.json in the root of project to explicitly using .net 6 SDK to build my project. However, I want a proper solution, and to know the reason behind this error.

Please note that I only experience this issue with dotnet watch run. I.e., other cases like dotnet build, dotnet run, dotnet publish are all fine!

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Mojtaba
  • 2,764
  • 1
  • 21
  • 24

2 Answers2

1

This is a duplicate of this one, that imho also has the wrong answer marked as true.

The correct answer should be this one, that links to the original documentation.

This is the expected behavior and a breaking change, because the .NET cli will always look for the latest version except in the case you define this in a global.json file.

João Pereira
  • 1,647
  • 15
  • 18
  • 1
    Thank you for pointing me to that thread, though I'd choose a better title to make it more searchable. But I still can't get my head around why they did such a design that `dotnet watch run` doesn't work but `dotnet run` does! The funny thing is that sentence in their documentation says "On rare occasions, you may need to use an earlier version of the SDK.....". Using `dotnet watch run` is not a rare case!! – Mojtaba Nov 21 '22 at 10:04
  • By the way, I liked that trick mentioned in the accepted answer `dotnet watch run xyz` the most! – Mojtaba Nov 21 '22 at 10:05
0

You will need to look into your web.config file or other configs that are in use. You are specifically looking for <assemblies> tags, so you will need to search for all occurrences of <assemblies in your repo. Look at all matches and see what their version is, the problematic assembly in your case is System.Runtime, but it's possible that you have other similar problems.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thanks. I have neither `web.config` nor any use of the term `assemblies` in my repo. – Mojtaba Nov 18 '22 at 13:40
  • @Mojtaba somehow your assemblies are specified. Do you have an AppFile.config? – Lajos Arpad Nov 18 '22 at 13:58
  • No. Usually there is no `web.config` or `appfile.config` in net core apps. BTW, if your theory is right, why the error does not occur for other commands, e.g., `dotnet build` or `dotnet run` – Mojtaba Nov 18 '22 at 16:18