Skip to content

Razor runtime view compilation via environment variable not working in .NET 6 #38465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AndrewTriesToCode opened this issue Nov 17, 2021 · 9 comments · Fixed by #39381
Closed
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views
Milestone

Comments

@AndrewTriesToCode
Copy link
Contributor

AndrewTriesToCode commented Nov 17, 2021

Runtime view compilation appears to not be working in .NET 6 Razor Pages projects when using the environment variable to specify this behavior as described here:

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-6.0&tabs=visual-studio#conditionally-enable-runtime-compilation-in-an-existing-project

If I manually add AddRazorRuntimeCompilation() in ConfigureServices it does work though.

I'm including v6.0.0 of the assembly package:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0" />

And my launch profile has the following section:

"environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }

Worked as expected in .NET 5 on the same project.

@davidfowl
Copy link
Member

cc @halter73 (might be a hosting problem)

@javiercn javiercn added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views labels Nov 17, 2021
@javiercn
Copy link
Member

@AndrewTriesToCode thanks for contacting us.

@pranavkm do you have any thoughts?

@ghost
Copy link

ghost commented Nov 22, 2021

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@pranavkm pranavkm added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Nov 22, 2021
@pranavkm pranavkm removed this from the .NET 7 Planning milestone Nov 22, 2021
@pranavkm
Copy link
Contributor

This was introduced as a result of my attempt to refactor the way action descriptors are registered for Razor. Runtime compilation requires adding a matcher policy and a different ADP, which is currently contingent on the default one being already registered: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Razor.RuntimeCompilation/src/DependencyInjection/RazorRuntimeCompilationMvcCoreBuilderExtensions.cs#L88-L99. This unfortunately does not happen with the env variable since the hosting startup runs before ConfigureServices. My guess is one way to fix it would be to reduce the check to only remove the ADP on Line 90, and do everything else all the time.

That said, for 6.0, we intentionally removed the option of conditionally enabling runtime compilation from our project templates because we believe hot reload is a better dev time alternative. Particularly for .cshtml files, you get really high fidelity for the kinds of edits that are supported, so there generally shouldn't be a reason to require this feature.

@pranavkm pranavkm removed their assignment Nov 22, 2021
@AndrewTriesToCode
Copy link
Contributor Author

Thanks for the details. My experience on Mac has been that hot reload only works on non-debug runs, causes an error, and is disabled for the rest of the session. It might be due to the fact that I use Rider so I’ll try it in VS code.

@SteveSandersonMS
Copy link
Member

@pranavkm Should this go on the backlog then? I'm putting it there but please feel free to move it if you disagree.

@SteveSandersonMS SteveSandersonMS added this to the Backlog milestone Nov 25, 2021
@ghost
Copy link

ghost commented Nov 25, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@knoepdan
Copy link

knoepdan commented Dec 13, 2021

In our Single Page App scenarios/setups, this is a crucial feature and hot reload does not replace it properly.
I get an error: "{"ErrorCode":5000,"Message":"Error in path , ex: InvalidOperationException\r\n System.InvalidOperationException: Cannot find compilation library location for package " when activating razor compilation in .Net 6 via Code (and not via enironment variable):
services.AddControllersWithViews().AddRazorRuntimeCompilation()
It's working in .Net 5.

No matter what I tried, it just doesn't work in .Net 6. In my opinion, the extension method "AddRazorRuntimeCompilation" should either be fixed or completly removed.

@DanielRBowen
Copy link

I have a project that I have yet to update to .Net 6 because of this problem. Hot reload isn't working either. Is it because I am debugging on IIS with VS that hot reload isn't working properly? If that is the case then I would like either for Hot Reload to work with when debugging with IIS or fix AddRazorRuntimeCompilation()

@pranavkm pranavkm modified the milestones: Backlog, 7.0-preview1 Jan 8, 2022
@pranavkm pranavkm self-assigned this Jan 8, 2022
pranavkm added a commit to pranavkm/aspnetcore that referenced this issue Jan 8, 2022
As part of refactoring how Razor Pages are loaded, the ability to use runtime compilation via the hosting startup / env variable was broken.
The refactoring assumed AddRuntimeCompilation was always invoked after a call to AddRazorPages / AddControllersWithViews etc which is not the case
for registration via the HostingStartup. This PR address this defect.

Fixes dotnet#38465
pranavkm added a commit that referenced this issue Jan 19, 2022
…39381)

As part of refactoring how Razor Pages are loaded, the ability to use runtime compilation via the hosting startup / env variable was broken.
The refactoring assumed AddRuntimeCompilation was always invoked after a call to AddRazorPages / AddControllersWithViews etc which is not the case
for registration via the HostingStartup. This PR address this defect.

Fixes #38465
pranavkm added a commit to pranavkm/aspnetcore that referenced this issue Jan 19, 2022
…otnet#39381)

As part of refactoring how Razor Pages are loaded, the ability to use runtime compilation via the hosting startup / env variable was broken.
The refactoring assumed AddRuntimeCompilation was always invoked after a call to AddRazorPages / AddControllersWithViews etc which is not the case
for registration via the HostingStartup. This PR address this defect.

Fixes dotnet#38465
ShreyasJejurkar pushed a commit to ShreyasJejurkar/aspnetcore that referenced this issue Jan 22, 2022
…otnet#39381)

As part of refactoring how Razor Pages are loaded, the ability to use runtime compilation via the hosting startup / env variable was broken.
The refactoring assumed AddRuntimeCompilation was always invoked after a call to AddRazorPages / AddControllersWithViews etc which is not the case
for registration via the HostingStartup. This PR address this defect.

Fixes dotnet#38465
wtgodbe pushed a commit that referenced this issue Feb 2, 2022
…39381) (#39639)

As part of refactoring how Razor Pages are loaded, the ability to use runtime compilation via the hosting startup / env variable was broken.
The refactoring assumed AddRuntimeCompilation was always invoked after a call to AddRazorPages / AddControllersWithViews etc which is not the case
for registration via the HostingStartup. This PR address this defect.

Fixes #38465
@ghost ghost locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants