DEV Community

Cover image for Hot Reload for .NET Developers
Daniel Gomez
Daniel Gomez

Posted on

Hot Reload for .NET Developers

Hey, there! This time we will have the opportunity to learn about one of the most anticipated features for .NET developers: Hot Reload.

What is Hot Reload?

Hot Reload allows us to modify the source code of our application while it is running and visualize these changes in real time without the need to restart execution.

How to access this new functionality?

At the time of writing (June 2021), you can access .NET Hot Reload from the 16.11 Preview version of Visual Studio 2019. With this release, we'll be able to open any project on the .NET ecosystem that's supported, and test this functionality.

Hot Reload in action

To test this functionality, let's look at an example of a web application with the DotVVM framework on ASP.NET, which is based on the MVVM (Model, View, Model View) pattern.

The steps are as follows:

  • Open the project in Visual Studio.
  • Start the application with the debugger associated with F5.
  • Open the file with the source code you want to edit.
  • Apply the changes in the code.
  • View the corresponding update.

To apply the changes made to the code, in Visual Studio 2019 we can locate the Hot Reload button as you can see below:

Hot Reload Button

Here's an example for changes to a class in C#:

GIF Hot Reload C Sharp

We can also apply it to other source code components, for example, html views in DotVVM:

GIF Hot Reload HTML

Fabulous, right? Now, you might be wondering what happens when we encounter run-time exceptions or compilation errors. Well, in those scenarios we will visualize something like this:

GIF Hot Reload Errors

Hot Reload Limitations

According to Microsoft documentation, the changes that you can make to the source code during a debugging session without restarting execution are as follows:

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

there are also changes that are not compatible with this functionality then you can view some of them

  • Rename the elements.
  • Removal of namespaces, types, and members.
  • Modification of interfaces.
  • Modification of method signatures.

On this web page you can see all the details regarding the limitations of Hot Reload in .NET:

Additional aspects to be commented on

The hot reload examples we just saw were applied to a project with .NET 5. This has been possible thanks to the fact that we have used the Visual Studio 2019 16.11 Preview.

Now, Microsoft has thought this functionality mainly for .NET 6, unlike our examples, we could visualize how our website is reloaded in real time with Hot Reload, also, if we wanted to use this functionality in the command console, for example, we would need at least the Preview 4 of .NET 6.

Currently (June 2021) we can only preview these features, for the future, Microsoft plans to get the full power of this feature with .NET 6 (and future versions of .NET) and Visual Studio 2022. At the moment, it is possible to mention that the new version of .NET will be announced at .NET Conf 2021, which will take place on November 9-11 of this year.

Thanks for reading

Thank you very much for reading, I hope you found this article interesting and may be useful in the future. If you have any questions or ideas that you need to discuss, it will be a pleasure to be able to collaborate and exchange knowledge together.

See you on Twitter! Or if you like you can also write to me on Instagram or Telegram. :=)

Top comments (0)