Introducing the .NET Hot Reload experience for editing code at runtime

Dmitry Lyalin

Today, we are excited to introduce you to the availability of the .NET Hot Reload experience in Visual Studio 2019 version 16.11 (Preview 1) and through the dotnet watch command-line tooling in .NET 6 (Preview 4). In the rest of this blog post, we’d like this opportunity to walk you through what is .NET Hot Reload, how you can get started using this feature, what our vision is for future planned improvements and clarity on what type of edits and languages are currently supported.

What is .NET Hot Reload?

With Hot Reload you can now modify your apps managed source code while the application is running, without the need to manually pause or hit a breakpoint. Simply make a supported change while your app is running and in our new Visual Studio experience use the “apply code changes” button to apply your edits.

.NET Hot Reload, apply code changes in Visual Studio 2019

Hot Reload works with many of your existing and upcoming project types such as WPF, Windows Forms, .NET MAUI previews, ASP.NET Core apps code-behind, Console applications, WinUI 3 (managed debugger required) and many others. This support is very broad with the core experience working with any project that is powered by .NET Framework or CoreCLR runtimes.

With Hot Reload our goal is to make this experience available no matter how you prefer to launch your app. With today’s release you can now use this experience through the fully integrated Visual Studio debugger experience or the dotnet watch command-line tool, with more options to come in later releases.

Getting Started

To get started you have the option of either using Visual Studio’s newest preview release or our .NET 6 Preview 4, instructions below.

Visual Studio:

To try Hot Reload in Visual Studio when using the debugger:

  • Download and install Visual Studio 2019 version 16.11 Preview 1
  • Open a supported project type, for example a WPF app
  • 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 code that can be re-executed through the running apps user interface (example: code-behind of a button or a ViewModel’s command) or something that is being triggered at an interval through a timer and change the code
  • Apply the code changes using the new apply code changes (ALT-F10) button in your Visual Studio toolbar (next to the Continue button). Note that saving the files is not required when using Visual Studio, giving you the flexibility to quickly change code and keep going.

If the change you made is supported your app will now be patched while its running with your new logic and you should see the changes in your app’s behavior the next time the updated code is re-executed by either your action or by something like a timer triggering the code.

You can also continue to use other debugger features such as breakpoints, Edit & Continue, XAML Hot Reload, etc. Everything that you are used to today should fully work in parallel with .NET Hot Reload. If something does not work for you, please let us know!

CLI

To try Hot Reload from the command-line when launching your app using dotnet watch:

  • Install .NET 6 Preview 4
  • Update your existing ASP.NET Core project to target .NET 6
  • Add the “hotReloadProfile”: “aspnetcore” property to your apps launch profile in launchSettings.json.

Example of Properties/launchSettings.json:

{
  "profiles": {
    "dotnet": {
      "commandName": "Project",
      "hotReloadProfile": "aspnetcore"
    }
  }
}
  • Run the project using dotnet watch and look at the output as it should indicate that hot reload is enabled
  • Make a supported code change to your apps managed source code and save the file to apply

Just like with the Visual Studio experience your new logic should now be applied and you should see the changes in your app’s behavior the next time the updated code is re-executed.

You can also use this approach with your Blazor WebAssembly projects by modifying the “blazorwasm” hot reload profile and following similar steps above. You can even try it with a Windows Forms or other types of projects powered by CoreCLR, just manually add a file named launchSettings.json under the Properties folder with the above example contents.

This experience is still in development, and we are tracking future improvement that will make it easier to use dotnet watch to Hot Reload all types of .NET Core apps without launchSettings.json files, but this is a limitation in the current release.

Best in Visual Studio 2022 & .NET 6

With today’s release this is just a preview of our full vision of Hot Reload for .NET developers. While some capability is being made available in early .NET 6 previews and in Visual Studio 2019, for the full power of this feature we are targeting .NET 6 (and future releases of .NET) and Visual Studio 2022 as the set of frameworks and tooling that will have the most complete and optimized experience.

To give you a glance into what type of features we plan to deliver in future previews and our final release here are some examples:

  • .NET Multi-platform App UI (.NET MAUI): With .NET 6 Preview 4 developers building .NET MAUI applications can now use .NET Hot Reload with projects targeting WinUI 3. In future releases we’re bringing .NET Hot Reload support to iOS, Android, and Mac Catalyst scenarios.
  • Razor Pages: In future releases Hot Reload and Edit and Continue (EnC) will be supported for editing Razor for websites or Blazor apps targeting .NET 6 or higher
  • No Debugger Required in Visual Studio: In future release of Visual Studio 2022 we are working to add support to use Hot Reload without needing the debugger, this means developers will be able to launch their apps using CTRL-F5 and still use Hot Reload to patch their running app
  • Reducing the number of unsupported changes: In future releases of Visual Studio 2022 and .NET 6+ we’re planning work across multiple teams that will reduce the number of unsupported edits at runtime
  • Optimizing frameworks to work best with Hot Reload: In .NET 6 we’re investigating how certain frameworks can be improved to better support Hot Reload. Examples of this in the future will include improvements to ASP.NET Core, .NET MAUI and other frameworks where tweaks and optimization will make Hot Reload changes more useful in more situations.

While the above are our current plans, note that plans can change based on customer feedback and schedule.

Supported/Unsupported changes and languages

No matter how you use .NET Hot Reload please be aware that some changes are not supported at runtime and will prompt you with a rude edit dialog and require you to restart your app in order to apply. We’re still working on the feature and the documentation to detail what edits are supported. For now, start by reviewing our existing list of Edit and Continue (EnC) equivalent capabilities. Since Hot Reload is powered by EnC this will give you a good starting point for better understanding this new feature. For details see: EnC documentation.

Also, while the above examples are specifically mentioning C#, Visual Basic is also supported in various situations when running under Visual Studio’s debugger. F# is currently not supported in .NET 6 but we are planning to support in a future release based on customer feedback.

Your feedback matters

In this early preview release, we want to acknowledge that there will be bugs. Sometimes if you try to apply a change it might silently fail, or your app might crash, etc. If you do encounter any problems, please take a moment to report issues to us, as only with your feedback can we ensure that critical problems are resolved, and future decisions are prioritized based on your input.

To reach us please use the Visual Studio feedback mechanism.

40 comments

Discussion is closed. Login to edit/delete existing comments.

  • Aleksander Kovač 0

    Hot reload is an awesome function even in this preview version. I am very excited regarding the planned features because it will get even better. So a lot of excitement here.

  • Daniel Meza 0

    Excellent job guys!

    Have an option to use Hot Reload without debugging is the right path, I hope to see this working on mobile platforms too.

  • Marco 0

    Hi Dmitry,
    very cool feature !

    Concerning when .NET 6 is released officially: Will this hot reload feature only be available on Windows or also be available in Visual Studio for the Mac ?

    • Dmitry LyalinMicrosoft employee 0

      Great question Marco. Being fully open, there is a very active internal team working to try and get this working for Visual Studio for Mac. It’s in our plans and we are really trying to get there.

      But being frank, there are a bunch of challenges still in front of us to get this done, so give us more time to see what we can do and we will communicate more details later this year.

      • anonymous 0

        this comment has been deleted.

  • MgSam 0

    I still don’t really understand the motivation for this feature. Edit and Continue almost never works in my experience. Is setting a breakpoint to use EnC really that onerous? Why not focus on making EnC work in 100% of scenarios?

    • Dmitry LyalinMicrosoft employee 0

      The good news in that sense is that we are making EnC better as you suggested. Think of Hot Reload and EnC as ‘experiences’ on top of a shared mechanism that we are making better. We know there are many limitations today and we’ll never be able to remove all of them, but our goal is to have a lot less unsupported edits as time goes on.

      One example of this is how we just added support for adding/changing using directives so this will now work in both EnC and Hot Reload. We have a lot more stuff like this coming.

      What we hope folks like you will start to do is report unsupported scenarios that are making your life difficult and less productive so we can better understand what customers are facing in their real-world apps. So please do use the VS feedback feature or even DM me on Twitter, we are listening.

      • Federico Berasategui 0

        Well, for one, EnC does not currently work if your method contains a switch expression, which is really annoying, since I’m rather fond of a lot of switch expressions (having used F# for a number of years). Is there a Github issue or somewhere where I can express my feelings about this?

      • Jiří Zídek 0

        It woule be very helpful if the error message does NOT spell “unsupported change” but rather “you changed this and this and we cannot change the code on the fly because it would break this or that”. Maybe developer can change the edits to avoid the problem, or at leat it would allow better understand where it cannot work and what is OK.

    • Jiří Zídek 0

      Same experience here. It worked before lambdas and linq has been introduced (VS 2008 I guess), then almost never…

  • Paulo Pinto 0

    While this is a very welcomed feature, maybe you should rename CLR into C# Language Runtime, given that only C# gets the .NET full development experience.

    • Charles Roddie 0

      Is there a way to do this in F# (or any .Net language) without Edit and Continue (which may take a long time to develop), by building and swapping in changed assemblies in a running app?

      • Paulo Pinto 0

        C# fits under any .NET language as well, the original idea of Common Language Runtime as presented in 2001 isn’t how .NET languages are being handled in modern .NET workloads hence my remark.

  • Jon Osborne 0

    I’ve installed the Visual Studio Preview, and .NET 6 preview too. I created a new project from the Blazor WASM template and followed the instructions but was not able to get the Hot reload capability to work. I tried changing the functionality of the Counter button (from ++ to –) as well as changing CSS definitions and pressing Alt F10, but nothing changed dynamically for me. I checked the debugging profile and it’s set as described, and am not sure what else to check as am so keen to try this functionality as it will be a huge time saver for me.

    • Kühberger Michael 0

      I had the exact same problem. The only way how I got it working was with

      dotnet watch

      .

      • Jon Osborne 0

        Thanks – that approach works for me, so a shame that the current preview doesn’t.

    • Rogerio Dalvi 0

      Same problem here.

      dotnet watch work but not for use with AOT.

  • Nobuyuki Iwanaga 0

    In my community, the “Apply code changes” button is called “You’re fired!” button, which is too ominous.

  • Alexey Leonovich 0

    Clarify MS intentions, please: does .NET 6 (I mean final version, not preview) development will be available exclusively in Visual Studio 2022 or it will be also possibe in future versions of Visual Studio 2019?

  • Nathaniel Elkins 0

    When you say F# is not supported, do you mean not officially supported (but might work in a janky kind of way), or doesn’t work at all in any capacity?

  • Bruce Garrick 0

    Why is it impossible to find and download the 16.11 Preview?

Feedback usabilla icon