Hot Reload in .NET

14 January 2022 at 10:00 by ParTech Media - Post a comment

One of the most impressive features of.NET 6 is hot reload, which was first introduced in the.NET 6 Preview 3 version. It enables us to change the UI while the program is running by modifying the code.

Without the need to restart the application, we can see the instantaneous reflection of those changes (after saving them) in the output. You can design apps rapidly, save time, and increase productivity using hot reload. WPF, Windows Forms, WinUI, ASP.NET Core, console apps, and other project types are compatible with hot reload.

In this post, we will understand everything about this interesting feature of .NET.

Table of contents

  1. What is Hot Reload in .NET?
  2. How to get started with Hot Reload?
  3. Other aspects about Hot Reload
  4. Final Thoughts

What is Hot Reload in .NET?

As stated above, Hot Reload allows you to make changes to your application's source code while it's running and see them in real-time without having to restart it. You can alter your app's managed source code while it's running without having to manually pause or reach a breakpoint, thanks to Hot Reload.

Simply make a supported modification while your app is running and utilize the "apply code changes" option in Visual Studio to apply the changes. WPF, Windows Forms,.NET MAUI previews, ASP.NET Core apps code-behind, Console programs, WinUI 3 (managed debugger required), and a variety of other project types are supported by Hot Reload. The support is quite comprehensive while working with any project that uses the.NET Framework or CoreCLR runtimes.

You may use this feature using the fully integrated Visual Studio debugger or the .NET watch command-line tool, with more options coming in future releases.

How to get started with Hot Reload?

Visual Studio

Follow the below steps to implement hot reload in Visual studio:

  • Open the project.
  • Start the application by pressing F5 to bring up the debugger.
  • Open the file containing the source code that you want to change.
  • Make the necessary adjustments to the code.
  • Take a look at the corresponding update.

If your change is supported, your app will be patched while it is running with your new logic. You should see the changes in your app's behavior the next time the updated code is re-executed, either by your action or by a phenomenon like a timer. Breakpoints, Edit & Continue, XAML Hot Reload, and other debugging capabilities are still available. Everything you're used to should operate well in tandem with .NET Hot Reload.

Command Line Interface

Follow the instructions below to try Hot Reload from the command line while launching your app using .NET watch:

  • Firstly, install .NET 6 Preview 4.
  • Now update your pre-existing ASP.NET project to target (which is .NET 6).
  • Add the property “hotReloadProfile”: “aspnetcore” to the app’s launch profile in launchSettings.json. Here is an example of Properties/launchSettings.json:
  • Use .NET watch to run the project and look at the output; it should show that hot reload is enabled.
  • Make a supported code modification to the managed source code of your app and save the file to apply the change.

Your new logic should now be applied, and you should observe the changes in your app's behavior the next time the revised code is re-executed, just like in the Visual Studio experience.

By altering the "blazorwasm" hot reload profile and following the instructions above, you can use this strategy with your Blazor WebAssembly projects as well. You can also try it with Windows Forms or any CoreCLR-based project by manually adding a file named launchSettings.json to the Properties folder with the contents shown above.

Other aspects about Hot Reload

According to Microsoft, you can make the following modifications to the source code without restarting execution during a debugging session:

  • Types
  • Iterators
  • Asynchronous expressions/await
  • LINQ Expressions
  • Lambdas
  • Dynamic objects

There are also certain updates that are incompatible with this capability, which you may see:

  • Renaming the names of elements
  • Namespaces, types, and members have been removed
  • Modifying the interfaces
  • Modifying the method signatures

Now, Microsoft considers this functionality to be primarily for.NET 6. With Hot Reload, you can see how your website is reloaded in real-time. If you wish to utilize this functionality in the command terminal, for example, you'll need at least the.NET 6 Preview 4 version.

These functionalities are now only available as a preview. However, Microsoft plans to release the full capability of this feature with.NET 6 (and future versions of.NET) and Visual Studio 2022.

Final Thoughts

Hot reload functionality allows you to update changes without stopping the process of debugging or restarting the program. This is a major advantage for all the developers out there. It cuts down on development time and boosts output.

Latest