CodeCaster
CodeCaster

Reputation: 151720

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

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:

Setup

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

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?

Upvotes: 7

Views: 1881

Answers (3)

WilliamK
WilliamK

Reputation: 1772

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! 😃

Upvotes: 1

James White
James White

Reputation: 1994

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

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151720

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.

Upvotes: 3

Related Questions