Razor Runtime Compilation breaks Hot Reload and therefore debugging in ASP.NET
Asked Answered
I

3

6

Nomenclature

This question is about three related subjects that allow the developer to introduce code changes in a running application without having to rebuild and restart said application:

  • Edit and Continue: Visual Studio's functionality to modify assemblies that are being debugged. You can edit managed code while on a breakpoint (within some constraints), and it'll magically be applied to the debuggee.
  • Hot Reload: introduced with Visual Studio 2022, kind of like Edit and Continue, enabling runtime recompilation of managed code without having to be paused on a breakpoint or even having a debugger running to begin with.
  • Razor Runtime Compilation: editing Razor views of a running application, by recompiling them on save of a .cshtml file.

Setup

The problems described below also occur on combinations of earlier versions of those components. Then:

  • Start Visual Studio 2022

  • Create an ASP.NET Core Web App running on .NET 6 or 7

  • Add the NuGet package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

  • Change the generated Program.cs code to the following to add Razor Runtime Compilation:

    // Add services to the container.
    var mvcBuilder = builder.Services.AddRazorPages();
    
    #if DEBUG
    mvcBuilder.AddRazorRuntimeCompilation();
    #endif
    

Reproduction

Now set a breakpoint in any view, Index.cshtml would be fine, and run the application.

As soon as the breakpoint is hit, change some Razor code. Or don't, the issues trigger from just having (multiple?) .cshtml files open as well.

Then hit Ctrl+S and F5 to apply your changes and continue running your application, and tada.wav:

How dare you

Hot Reload can't automatically apply your changes. The app needs to be rebuilt to apply updates.

Alternatively, change some code in the code behind (.cshtml.cs). Now you will get random NullReferenceExceptions or ExecutionEngineExceptions when continuing.

Workaround

Close all .cshtml files before starting a debug session.

Questions

Is it possible to:

  1. Get some confirmation that I'm not the only one that encounters this?
  2. Have "Edit and Continue" without "Hot reload"? The settings for those seem to have been combined, it's either all or nothing.
  3. Make this (editing Razor files and C# code while debugging) work without getting these dreaded errors?
  4. How can I get Microsoft to set the Cancel key (Esc) to the Continue Editing button?
Insulting answered 15/11, 2022 at 10:59 Comment(4)
github.com/dotnet/aspnetcore/issues/38809Insulting
#69778772Insulting
I have these same problems. Additionally I've also had problems if i hit a breakpoint, change a line to use edit and continue, and then save (with hotreload on save enabled). I've found hotreload on save is best avoided, trigger hotreload manually when needed only.Mussorgsky
Oh I thought I read my specific issue but mine is from a breakpoint in .cs files. When I make a view change, Ctrl+S and F5, and then hit any breakpoint in a .cs file, F10 will not continue and I get the same hot reload must rebuild error.Mussorgsky
I
2

It would appear that not having Razor Runtime Compilation (Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation) installed in the affected project largely mitigates this error (though the runtime still throws the occasional NullReferenceException/ExecutionEngineException while trying to reproduce the issue do my work).

As Hot Reload will also recompile your Razor views, simply uninstall the RuntimeCompilation package and you should get fewer errors.

Edit: this is not entirely true, the error popup still gets shown, albeit less frequently.

Insulting answered 15/11, 2022 at 10:59 Comment(2)
It's unfortunate RuntimeCompilation doesn't play nicely, it's definitely faster to make a view change and see it. Hot reload does work it's just slower. I've found a way to leave RuntimeCompilation on and not have breakpoints w/ Ctrl+S and F5 cause errors. Instead of F5 to trigger the RuntimeCompilation, click Hot Reload, that way RuntimeCompilation is never engaged. I plan to do this for most debugg sessions, and when I know I need to iterate quickly on only view changes, I'll use RuntimeCompilation with w/ Ctrl+S and F5.Mussorgsky
Yeah Hot Reload is definitely slower than Ctrl+S and refreshing the page, but it is what it is. The former does allow you to make changes while not even on a breakpoint though, so it has additional benefits.Insulting
O
1

A solution for us was disabling Progress/Telerik JustMock Profiling.

I was trying to Hot Reload changes on .cshtml while debugging, but then the message appears:

Hot Reload can't automatically apply your changes. The app needs to be rebuilt to apply updates. Visual Studio - Can't Hot Reload Message

Also, at the error list another information appears:

Visual Studio - COR_ENABLE_PROFILING

ENC2018 Changes made in project 'Project.Name' require restarting the application: Changes are not allowed when 'COR_ENABLE_PROFILING' environment variable is set.

After investigating a little bit further, I looks like the Registry key COR_ENABLE_PROFILING (Regedit) at Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\.NETFramework is affecting the Hot Reload of Visual Studio 2022.

Disable JustMock Telerik

The extension Telerik JustMock we use in Visual Studio 2017 is responsible for create this keys. After disable it on VS 2017, or VS 2022, and restart the application in debug mode, the application works with debug and hot reloading features! 😃

Oscitancy answered 9/3, 2023 at 18:10 Comment(0)
C
0

Things to try when Hot Reload won't work:

1.) Check the 'Hot Reload' output window and Error List for clues to why hot reload failed. For instance I found an error about having a 'COR_ENABLE_PROFILING' environment variable in the error list after getting the usual dialog. This appeared in the VS2022 17.5 Preview 6.0 and I'm not sure if it was there before.

2.) Make sure COR_ENABLE_PROFILING is disabled in your environment variables.

3.) Search the entire project for RuntimeCompilation (could appear in web.config, launch properties, packages) and remove them.

4.) Disable 'Native Code' debugging in your debugging profile.

5.) Restart your PC and check again

Clinic answered 15/2, 2023 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.