Razor Runtime Compilation breaks Hot Reload and therefore debugging in ASP.NET - asp.net-core

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
Visual Studio 2022 (17.4.4)
.NET 7 (SDK: 7.0.102) or 6 (SDK: 6.0.403)
NuGet: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
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:
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:
Get some confirmation that I'm not the only one that encounters this?
Have "Edit and Continue" without "Hot reload"? The settings for those seem to have been combined, it's either all or nothing.
Make this (editing Razor files and C# code while debugging) work without getting these dreaded errors?
How can I get Microsoft to set the Cancel key (Esc) to the Continue Editing button?

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.

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

Related

Rider Hot Reload for .NET Core WebApi not working

So basically If I make a code change, the C# application won't restart, I had to rerun manually the build project everytime I add a new line of code. This is on JetBrains Rider IDE.
So as they say in documentation, this feature is enable by default if I'm using .NET Core 6+ (which I do), I also have all checks
Any ideas so far? Or am I missing something and I need to rerun everytime I change something on the Controller with this IDE?

.NET Core MVC app not updating View unless entire project is published

I have a .NET Core MVC app hosted in IIS (development) as well as Azure App Service (production).
When I make a simple HTML change to a Razor View and publish just that view, it does not get updated.
It only gets updated if I publish the entire project.
This happens in both IIS and Azure app service.
Is this the default behavior or am I doing something wrong?
Here is the configuration page from Azure App Service:
When you publish the complete program to iis, iis compiles and runs it. .net core mvc disables runtime compilation by default, so even if the view is updated and released, the program that is already running will not compile the new view.
If you want iis to use the new view after the VS update and release the view, you can add a line of code to the startup to enable the function of compiling and running.
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Nuget package to the project.
Add following code in startup.cs:
services.AddRazorPages().AddRazorRuntimeCompilation();
Publish entire project.
After all of these, once you update view and publish. IIS will display new view.
Here is my test result.
You don't need to do any operations on the portal.
The article provided by Lex Li talks about the content of Razor when compiling. Simply put, C# server code can be written in the .cshtml file. After compilation, it will become projectname.Views.dll, so when you modify When the .cshtml file is not sure which projects it is associated with, it is recommended to update it globally to avoid bugs caused by version issues.
Regarding your current problem of partial update, it is also easy to solve. First, you need to define the file or folder inclusion options when compiling. You need read offical document first.
Below screenshot is my test project.
After my modification and settings, you can publish your customized files or folders to the azure production environment. (The code setting part is for reference only, coding according to specific projects)
In the post, you said that you want to publish an html file, then you can right-click the file or folder and select publish file or folder.
Right click test folder.
From the screenshot message below, we can see that the partial update was successful, the speed is very fast, the modified content is also prompted, and the global update is not performed.
prompt:
The above steps are all tested and passed, and the answers and code parts given are for reference only.
If you encounter problems during the operation, it is recommended to raise a support ticket on the portal.

Is it possible to change .cshtml View and see the changes instantly using .NET Core?

Context
In my .NET Framework 4.x ASP.NET MVC projects, when using the Visual Studio IDE, it was possible to edit a .cshtml view, save, then press ctrl+F5 in the browser and see the change immediately.
This seems to be no longer work in ASP.NET Core applications (using Visual Studio 2019 and .NET Core 3 Preview 5).
Question
Is this feature missing in ASP.NET Core? Is this a preview issue? Or am I missing something?
This is something that is no longer enabled by default as of ASP.NET Core 3, but it can be re-enabled as documented here:
Runtime compilation is enabled using the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. To enable runtime compilation, apps must:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:
services
.AddControllersWithViews()
.AddRazorRuntimeCompilation();
I've marked Kirk's answer as accepted, but maybe this experience could be useful also.
My goal was a "quick edit (cshtml/css/js/ts) then see" iteration.
No need to add and mod anything... I discovered that .NET Core (3) and VS 2019 is so much faster, so in case if we do not want to debug in VS (which is the scenario in most cases when we are changing cshtml/css/js/ts) there is really great iteration:
Press ctrl+f5 to launch your web app (no debug) in browser
See, test (debug js/ts in Chrome)
Edit your cshtml/css/js/ts
Press ctrl+shift+b to build (fast!)
Switch to your browser and press ctrl+f5 to refresh your page (fast!)
Goto 2

ASP.NET 5 - beta8: no rebuild in background

Before beta8 it was possible to start a Web Application, change some of the code (e. g. the ViewBag.Message of the About-View), save and refresh the browser - voilá, the new Message is displayed without rebuilding the project.
Now when creating a new Web Application with beta8 which uses DNX and Kestrel this seems no longer to work. Any idea why?
Beta8 contains a new library called 'dnx watch' which monitors your project files for changes during execution and automatically rebuilds the project. Install it by running the following from a command prompt:
dnu commands install Microsoft.Dnx.Watcher
Additionally, make sure you've installed the beta8 web tools for Visual Studio.
Finally, make sure you're not running in debug mode as code changes will not reload projects while running with the debugger attached. CTRL+F5 will start your web project without the debugger.
You can read up on this and the other changes in beta8 here:
http://blogs.msdn.com/b/webdev/archive/2015/10/15/announcing-availability-of-asp-net-5-beta8.aspx

What effect does the new precompile during publishing option have on MVC4 applications?

So I recently updated Visual Studio 2012 to Update 2. Lo and behold, the next time I go to publish my application (via File Publish in this case) I notice that there are three new options:
Delete all existing files prior to publish
Precompile during publishing (with a link to Configure)
Exclude files from the App_Data folder
The first and third options are pretty self-explanatory, but I can't find any documentation on the second option as it applies to MVC. When I check it, there doesn't seem to be any change in the files produced on the site and I don't see any real change in performance.
Using the ASP.NET precompiler can have the following impact on your MVC app:
If you have anything in App_Code, it will be precompiled into a DLL before deployment. Without precompiling, this would happen on the fly by the ASP.NET runtime.
If you choose the option to not make your pages updateable (i.e. uncheck the first checkbox in the advanced settings dialog), it will also precompile your views (ASPX and Razor) instead of compiling those dynamically at runtime as well. The default (checked) setting of "Allow precompiled site to be updateable" allows you to update your view content without needing to rebuild the entire project.
If you don't have any files in App_Code and you want your site to remain updateable, it doesn't seem to do much.
It is an old question, but I just encounter similar issue and feel something worth sharing.
My error message is same in this post. My project is MVC5, build with Visual Studio 2013 professional.
Compilation Error: The type 'ASP.global_asax' exists in both DLLs
In my case, with precompile option, there is a file, App_global.asax.dll, in bin folder, and cause above error message.
First, I remove App_global.asax.dll on server, restart application pool, issue is gone.
Then I tried another approach, uncheck precompile and republish, redeploy to server, issue is gone.