Reputation: 151720
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:
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
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:
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.
Close all .cshtml files before starting a debug session.
Is it possible to:
Upvotes: 7
Views: 1881
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.
Also, at the error list another information appears:
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.
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
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
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