What is Hot Reload?
Hot Reload in .NET works by applying code changes, which includes changes to stylesheets, and mostly to a running app without having to restart the app and without losing app state. Hot Reload is supported for ASP.NET Core version 6.0 and later versions used for building projects. This feature is great for fixing bugs quickly even while running the application. Some people call it, “Edit and Continue”.
What is the Significance of Hot Reload?
In the earlier versions of .NET and .NET Core, this feature was not present. What that meant was that you have to stop running your application if and when you discover some bugs, or you need to make a change to your code while debugging. The constant stopping and starting of the application can be, in fact, herculean and uninteresting. Hence, the introduction of the Hot reload within the Visual Studio IDE.
However, Hot Reloads may not work for every pause and fix while debugging your code. If you missed including a significant part of the code before debugging, you may need to stop running your application to include that before running. This is because the last instance of your build before effecting a change, would still be present.
You cannot include a service or middleware you forgot to put while coding, when debugging by using Hot Reload. The compiler is likely not going to pick that change. It’s safe to see the Hot Reload feature as an emergency edit and continue tool, and not used for inputting code blocks that should have been present earlier.
Frameworks That Support It
Hot Reload works with many familiar and upcoming project types such as WPF and Windows Forms, .NET MAUI previews, ASP.NET Core apps code-behind, Console applications, Win UI 3 (managed debugger required), and much more. This support is quite detailed with the core experience of working with any project that is powered by .NET Framework or Core CLR runtimes.
The goal of introducing the Hot Reload is to make this experience available no matter the preference of launching the app. The current release makes it possible to experience the feature through the fully integrated Visual Studio debugger or the .NET watch command-line tool, with more options to come in later releases.
Getting Started with Hot Reload in Visual Studio
To get started, the option of either using Visual Studio’s newest preview release or the .NET 6 is available. See the instructions below.
To use Hot Reload in Visual Studio with the debugger, do the following:
- Download and install Visual Studio 2022.
- Open a supported project type, for example an ASP .NET Core Web API, or a class library.
- Launch the app with the debugger attached through F5 (make sure that “enable native code debugging” is disabled in debugger settings/debug launch profile)
- Open a C# code file with some pre-written code that can be re-executed through the running apps user interface.
- Apply your code changes using the new apply code changes (ALT-F10) button in your Visual Studio toolbar (next to the Continue button).
Finally, note that saving the files is not required when using Visual Studio, giving you the flexibility to quickly change code and keep going.