使用热重载运行dotnet watch -c $(Configuration)。

8

我想要使用特定配置启动我的Blazor应用程序,并启用热重载。

当我使用以下方式启动时:

dotnet watch

热重载已启用,一切正常。

当我使用以下方式启动时:

dotnet watch run -c Simulation

热重载未激活,当文件更改时应用程序将重新构建。

我尝试过以下启动方式:

dotnet watch -c Simulation

但它返回 "watch: 以错误代码1退出"。

如何使用所需配置和热重载启动我的应用程序?

编辑:

我在launchSettings.json文件中的启动配置文件:

 "Dotnet Watch": {
  "commandName": "Executable",
  "executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
  "commandLineArgs": "watch run -c $(Configuration)",
  "hotReloadProfile": "aspnetcore",
  "workingDirectory": "$(ProjectDir)",
  "launchUrl": "https://localhost:5001;http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

"hotReloadProfile": "aspnetcore" 不起作用。

我的 dotnet 版本是:

6.0.300-preview.22154.4

EDIT2:

目前我使用环境变量而非项目配置来切换仿真档案和其他档案,但是没有任何解决方案可以在特定档案下进行热重载,这很奇怪,在网上也没有相关资料。


我不确定我能够理解你的逻辑。当 dotnet.exe 运行一个项目时,它会查询 launchsettings.json 文件以了解如何运行该项目。在这种情况下,当你执行 dotnet run 命令时,dotnet 会调用观察模式来运行你的项目,但这本身就会创建一个无限循环。为什么不将你的 json 文件更改为默认值,然后尝试从终端运行 dotnet watch run -c [...] 呢? - Andreas M.
@AndreasM。这个启动设置是为了IDE而设计的,但我已经测试过默认的launchSettings.json文件,也没有改变任何东西。当我使用run运行dotnet watch时,我没有激活热重载。我已经尝试过只添加你建议的配置文件,并使用dotnet watch run -c Simulationdotnet watch run --property:Configuration=Simulationdotnet watch run -c Simulation --launch-profile "dotnet"来启动,但每次保存时都会重新构建项目,热重载始终未激活。 - Okan S.
@AndreasM。我不知道,我不使用预览版,希望其他人可以确认一下。如果在那个补丁中修复了,那将是非常好的消息!不过在那之前我们需要一个解决方法...... - lonix
1
根据我的编辑答案,它适用于此补丁...我不知道非预览版本-OP似乎也使用预览-所以我问了一下。 - Andreas M.
@AndreasM,那真是个令人兴奋的消息!你知道预览版何时发布吗?我在任何地方都找不到相关信息。 - lonix
显示剩余2条评论
1个回答

4
根据 这篇文章,最好的方法是将"hotReloadProfile": "aspnetcore"添加到你的launchSettings.json 中,像这样:
{
  "profiles": {
    "dotnet": {
      "commandName": "Project"
    }
  }
}

使用dotnet watch run命令来调试你的应用程序。

编辑:

从dotnet sdk版本6.0.300-preview.22204.3开始,这个问题已经得到了解决,并且在第一个编辑中显示的配置可以在Visual Studio和JetBrains Rider中使用。在一个新的假配置Simulation中测试,直接从debug中复制过来。

话虽如此,似乎dotnet cli不能调用这个启动配置文件。

PS P:\Path\To\Project> dotnet run --launch-profile "Dotnet Watch"
The launch profile "Dotnet Watch" could not be applied.
The launch profile type 'Executable' is not supported.

我的完整的launchSettings.json文件:

{
  "profiles": {
    "HotRelaodTests": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Dotnet Watch": {
      "commandName": "Executable",
      "executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
      "commandLineArgs": "watch run -c $(Configuration)",
      "workingDirectory": "$(ProjectDir)",
      "launchUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

编辑 #2

在阅读了@mlhDev的评论后,我再次尝试了这个配置,使用的是.NET 6.0.400(稳定版,非预览版),热重载似乎可以工作。

查看GIF PoC


1
请查看问题的编辑,添加"hotReloadProfile": "aspnetcore"没有起作用,也没有改变任何东西。 - Okan S.
这已经不再回答问题了 - 你删除了 hotReloadProfile,而你的结论是 "dotnet cli 无法调用此启动配置文件"。 - mlhDev
当我能够访问我的计算机时,我会进行更多测试,然后相应地编辑我的答案。 - Andreas M.
1
@mlhDev,请参考我的Edit #2 - Andreas M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接