2022年12月3日星期六

安装.Net7 SDK后,之前的.Net6的项目执行dotnet watch命令时报错

今天git clone一项目,用VS2022打开后发现是.net7的,马上去微软官网下载安装.Net7 SDK,安装完.Net7 SDK后,再次运行之前的.Net6的项目执行dotnet watch run命令时报错,具体报错信息如下:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 系统找不到指定的文件。

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()

dotnet watch ❌ Exited with error code -532462766

dotnet watch ⏳ Waiting for a file to change before restarting dotnet...

临时解决方案: 在.Net6工程运行目录下建立 global.json 文件指定编译框架

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

上文件中的version的6.0.403根据自己电脑中已安装的.Net 6.0 SDK版本自行修改。

如果你的电脑没有6.0.403版本,将会出现如下提示:

The command could not be loaded, possibly because:

  * You intended to execute a .NET application:

      The application 'watch' does not exist.

  * You intended to execute a .NET SDK command:

      A compatible .NET SDK was not found.

Requested SDK version: 6.0.203

global.json file: d:\71620.org\global.json

Installed SDKs:

3.1.425 [C:\Program Files\dotnet\sdk]

5.0.408 [C:\Program Files\dotnet\sdk]

6.0.306 [C:\Program Files\dotnet\sdk] (提示已经安装了.Net SDK 6.0.306版本,替换成这个就可以。或者可以去下载安装.Net SDK 6.0.403版本

7.0.100 [C:\Program Files\dotnet\sdk]

Install the [6.0.203] .NET SDK or update [d:\71620.org\global.json] to match an installed SDK.

Learn about SDK resolution:

https://aka.ms/dotnet/sdk-not-found

留下您的评论