Skip to content
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

[Discussion] Breaking changes to runtime compilation for Razor views and Razor Pages #7647

Open
pranavkm opened this issue Feb 16, 2019 · 14 comments

Comments

@pranavkm
Copy link
Member

commented Feb 16, 2019

As a consequence of cleaning up the ASP.NET Core shared framework to not depend on Roslyn, support for runtime compilation of Razor views and Razor Pages is being moved to a separate package.
Applications that require runtime compilation or re-compilation of Razor files should

  • Add a reference to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package
  • Update the application's ConfigureServices to include a call to AddMvcRazorRuntimeCompilation:
services.AddMvc()
    .AddMvcRazorRuntimeCompilation();

The following APIs previously available on Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions to support runtime compilation would now be available via
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions:

  • RazorViewEngineOptions.FileProviders -> MvcRazorRuntimeCompilationOptions.FileProviders
  • RazorViewEngineOptions.AdditionalCompilationReferences -> MvcRazorRuntimeCompilationOptions.AdditionalReferencePaths

In addition, Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions.AllowRecompilingViewsOnFileChange has been removed. Recompilation on file changes is enabled by default by referencing the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package.

Related issues: aspnet/Announcements#312, aspnet/Announcements#325

@mqudsi

This comment has been minimized.

Copy link

commented Feb 16, 2019

(This definitely deserves to be in the migration docs for the 3.0 jump.)

Any word on when the package will be available via nuget?

EDIT: I accidentally a word.

@pranavkm

This comment has been minimized.

Copy link
Member Author

commented Feb 16, 2019

@mqudsi It'll be available as part of the 3.0.0-preview3 release.

@mqudsi

This comment has been minimized.

Copy link

commented Feb 16, 2019

Thanks, @pranavkm. Looking forward to it.

@ctolkien

This comment has been minimized.

Copy link

commented Feb 17, 2019

Is this only for env == "Production" scenarios? I assume this doesn't change the development loop (whereby Razor files are recompiled on the fly?)

@mqudsi

This comment has been minimized.

Copy link

commented Feb 17, 2019

No, this applies everywhere (both in my understanding and from what I'm seeing in the latest preview). I'm having to restart IISExpress after each change to get the CSHTML changes to reflect.

@thewebchameleon

This comment has been minimized.

Copy link

commented Mar 7, 2019

This has been resolved with the release of preview 3; however, I am getting the exception below (using the template for MVC in VS2019)

image

adding the nuget package Microsoft.AspNetCore.Hosting.Abstractions does nothing

@xt0rted

This comment has been minimized.

Copy link

commented May 3, 2019

After adding a reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation I get a version mismatch on the Microsoft.CodeAnalysis.Common package. This is because Microsoft.VisualStudio.Web.CodeGeneration.Design is referencing an older version. I had to remove the reference to Microsoft.VisualStudio.Web.CodeGeneration.Design otherwise view recompilation was throwing an exception.

@pixeldm

This comment has been minimized.

Copy link

commented Jun 20, 2019

Is there any way to set AddMvcRazorRuntimeCompilation() for only the dev environment? Currently, the only way to get design time recompiling of a view is to turn this feature on. Otherwise, the project must be restarted to see any changes to a view. However, enabling this feature also involves adding the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. This package adds a considerable amount of files to the publish folder when publishing. Furthermore, I don't really want runtime compilation of views for production. Currently, I can't find any way to limit this feature to just development. Any suggestions?

@dazinator

This comment has been minimized.

Copy link

commented Jun 20, 2019

@pixeldm
Maybe you could make that package reference conditional.

In your csproj

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <DefineConstants>UseRazorCompilation</DefineConstants>
</PropertyGroup>

Then in your startup, use pre processor directive

#if UseRazorCompilation
.AddMvcRazorRuntimeCompilation();
#endif

And also in your csproj add that condition to the package reference!

@pixeldm

This comment has been minimized.

Copy link

commented Jun 20, 2019

Maybe you could make that package reference conditional.

Thanks @dazinator ! This does indeed work as expected. Much appreciated for the help.

@kle622

This comment has been minimized.

Copy link

commented Jun 22, 2019

I'm just trying this out now, and it is working as described. Thanks!
Is there a way to watch for new .cshtml files to compile?

@janleon

This comment has been minimized.

Copy link

commented Jun 29, 2019

Will this be available for .net standard 2.0?

I currently have my razor view compilation in a .net standard 2.0 class library referenced in the web app and when trying to upgrade my web app to use .net core 3 it fails with the following error message:

MissingMethodException: Method not found: System.Collections.Generic.IList1<Microsoft.Extensions.FileProviders.IFileProvider> Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions.get_FileProviders()

I am unable to reference the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package in my class library as this package depends on netcoreapp 3.

The code I have working on .net 2,1 atm is based on https://github.com/mikebrind/RazorEngineViewOptionsFileProviders.

What is the best upgrade path to get this working again with net core 3.0 and still have my razor compilation code in a separate library using net standard 2.0?

@pranavkm

This comment has been minimized.

Copy link
Member Author

commented Jul 1, 2019

@janleon #3757 (comment) has details on what library authors need to do to reference parts of AspNetCore. Referencing ASP.NET Core or it's libraries requires targeting netcoreapp3.0, so you have to cross-compile to your library.

@lourencocotta

This comment has been minimized.

Copy link

commented Oct 3, 2019

services.AddMvc().AddRazorRuntimeCompilation();

that what worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants
You can’t perform that action at this time.