dotnet watch run -c $(Configuration) with hot reload
Asked Answered
E

1

8

I want to launch my Blazor app with specific configuration and have hot-reload enabled.

When I launch with:

dotnet watch

Hot reload is enabled and everything it's ok.

When I lanch with:

dotnet watch run -c Simulation

Hot reload is not active and the app rebuild when a file is changed.

I have try to launch with:

dotnet watch -c Simulation

But it's return "watch : Exited with error code 1".

How launch my app with wanted configuration and hot-reload ?

EDIT:

My launch profile from launchSettings.json file:

 "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"
  }
}

Adding "hotReloadProfile": "aspnetcore" doesn't work.

And my dotnet version is :

6.0.300-preview.22154.4

EDIT2:

Temporary I use environment variable and not project configurations to switch between Simulation profile and others, but it's strange to not have any solution's to have hot reload with a specific profile. Nothing on the web about that.

Eastwood answered 27/4, 2022 at 12:12 Comment(7)
I'm not sure i can follow your logic, here. When dotnet.exe runs agains a project, it consults launchsettings.json about how to run the project. In this case, when you dotnet run, dotnet will ... call dotnet to watch-run your project (?) but, this on itself will create an infinite loop. Why don't you change your json to the default values and try running dotnet watch run -c [...] from terminal?Hakenkreuz
@AndreasM. This launch settings is for IDE, but I have tested with default launchSettings.json file and it's change nothing too. When I run dotnet watch with run I do not have hot-reload activated. I have tested with adding only your suggested profile on your answer and launch with dotnet watch run -c Simulation, with dotnet watch run --property:Configuration=Simulation, with dotnet watch run -c Simulation --launch-profile "dotnet" and all the time hot-reload is not activated, the project is rebuild ate every save.Eastwood
@OkanS. Did you actually manage to get it working, even with a workaround? Maybe you can show us your working config? I am struggling also, for days.Chaeta
does the issue remains unfixed with dotnet version 6.0.300-preview.22204.3?Hakenkreuz
@AndreasM. I don't know I don't use previews, hopefully someone else can confirm for us. Is it expected to be fixed in that patch - because that would be very good news! We need a workaround until then though...Chaeta
As per my edited answer, it works for this patch... I don't know about non-preview versions -OP seems to use preview as well- so, that is why i askedHakenkreuz
@AndreasM. That is very exciting news! Do you know when that preview will be released? I couldn't find info anywhere.Chaeta
H
4

According to this article, the way to go is to add "hotReloadProfile": "aspnetcore" to your launchSettings.json like so:

{
  "profiles": {
    "dotnet": {
      "commandName": "Project"
    }
  }
}

And debug your app using dotnet watch run command.

Edit:

As of dotnet sdk version 6.0.300-preview.22204.3 this issue has been addressed and the configuration shown at your first edit works with both Visual Studio and JetBrains Rider. Tested on a vanila project with a new dummy configuration called Simulation copied directly from debug.

That said, it seems like dotnet cli cannot call this launch profile

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.

My Full 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"
      }
    }
  }
}

Edit #2

After reading the comment from @mlhDev, i tried this configuration again, using .NET 6.0.400 (stable, not pre-release) and hot-reload seemed to work.

See GIF PoC

Hakenkreuz answered 27/4, 2022 at 12:41 Comment(4)
Please see the question's edit, adding "hotReloadProfile": "aspnetcore" doesn't work and change nothing.Eastwood
This doesn't answer the question any more - you removed hotReloadProfile and your conclusion is "dotnet cli cannot call this launch profile"Crackle
I'll do some further testing when I have access to my computer, and edit my answer accordinglyHakenkreuz
@Crackle , Please reffer to my Edit #2Hakenkreuz

© 2022 - 2024 — McMap. All rights reserved.