Search code examples
asp.net-core.net-corehot-reload

Is hot reload supported for ASP.NET Core 6 Web API controllers?


I don't know if this a supported feature or not. I used dotnet new webapi to test the concept. I am using the .NET 6 SDK. The generated code includes a WeatherForecast controller.

I cannot get hot reload working for the code in the controller. I run the app with dotnet watch run. I am told "Hot reload enabled".

I then edit the static string array for the weather summaries and save the changes. I am told:

File changed: .\Controllers\WeatherForecastController.cs.'

and

Hot reload of changes succeeded

But when I call the API at https://localhost:7103/api/WeatherForecast, the changed values are not used. I know that it is not a caching issue because it works the same if I switch browsers to see the results of the call after the changes.

I know my setup is fine because hot reloading works if I comment out the content of program.cs and replace it some calls to write lines to the console.

I checked the article Live Code Editing - Supported changes but I do not see where it either should or should not work.


Solution

  • The simple answer is YES.

    My problem was that my code changes are apparently not changes that will be updated during a hot reload. As I said in my question, I edited the "static string array for the weather summaries".

    On the page Live Code Editing - Supported changes, it specifically states that one of the unsupported changes is to "Modify a static member after it has already been accessed by executing code". That is exactly what I was doing.

    I make a different change (to the temperature calculation) and this change was implemented in the reloaded code.

    But the problem had nothing to do with whether I was using Visual Studio, VsCode or just a text editor. Hot reloading still worked in all cases. In fact one of the things that I tried before finding the solution was to use VS2022. This also would not work if editing the static value.

    However, you cannot use hot reload together with debugging in VsCode. You can use either feature, both not both together at the same time. This is only available in VS proper.