I suspect this feature is working as designed. Roslyn assemblies (those prefixed with Microsoft.CodeAnalysis.*) were removed from the shared framework as of 3.0. Those assemblies are needed to support runtime view compilation.
Have you already tried adding a Condition
to your package ref? For example:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />
Then in your Startup
class, you could do something like the following:
public void ConfigureServices(IServiceCollection services)
{
IMvcBuilder builder = services.AddRazorPages();
#if DEBUG
if (Env.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
#endif
services.AddServerSideBlazor();
services.AddGrpc();
/cc: @pranavkm
I will give that a try but it would be nice if it would automatically in Release mode not include those since there are no CSHTML changing on a production machine.
@pranavkm Do you have any other ideas?
I will give that a try but it would be nice if it would automatically in Release mode not include those since there are no CSHTML changing on a production machine.
Conditionally referencing the package based on Configuration is the best option there is now.
Cool, trying it out. Do I have to do anything with the builder in the example or does it just work by adding properties to it? e.g. builder.AddRazorRuntimeCompilation();
I'm working on updating the doc right now. Here's a more complete example in the meantime: https://github.com/scottaddie/ContosoLending/blob/master/src/ContosoLending.Ui/Startup.cs#L31
Just a heads up before you go through all that work. I added Condition="'$(Configuration)' == 'Debug'" and the Startup AddRazorRuntimeCompilation() still seems to be working in Release mode.
Interestingly enough the Release Publish does understand it is not included, strange.
@cgountanis Did you also add the Startup.ConfigureServices
change I mentioned above? That should prevent AddRazorRuntimeCompilation
from executing in Release mode.
Will do just wanted to test it it parts. Figured I would get an error.
Ahhh, once I cleared out the /BIN folder it did give me the expected error ''IMvcBuilder' does not contain a definition for 'AddRazorRuntimeCompilation'". Just had those old files in there fooling me :)
I am currently using AddControllersWithViews() not AddRazorPages(), assuming I can just add the DEBUG logic for the AddRazorRuntimeCompilation() or should I change the way I am setting up. This is a basic MVC with EndPoints on Core 3.1, pretty much converted everything but I am still leaning on Razor pages for my CSHTML Views.
BTW, thanks for the help on this!
Maybe add both, just never needed AddRazorPages() before. Good practice to add all?
Edit: Looks like AddControllersWithViews() includes Razor?
In place of the AddControllers() extension method, this time we have AddControllersWithViews. As you might expect, this adds the MVC Controller services that are common to both Web API and MVC, but also adds the services required for rendering Razor views.
Edit: Think it explains it well enough here, but my application was working just fine without AddRazorPages(), guessing since I do not use "Pages" just use Razor within CSHTML in MVC style website.
Guess this might be nice for the documentation as some people use Pages and many more are using MVC with Razor syntax within CSHTML which is what I prefer on large MVC web site applications.
It sounds like there's no need for you to call services.AddRazorPages();
. Call services.AddControllersWithViews();
instead. You would only need the AddRazorPages
method call if you have Razor Pages files (those .cshtml files which are most often stored in a *Pages directory and that contain an @page
directive as the first line in the file).
OK, seems to be working, in the end I ended up with this:
CSPROJ
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />
STARTUP
public void ConfigureServices(IServiceCollection services)
{
IMvcBuilder builder = services.AddControllersWithViews();
#if DEBUG
builder.AddRazorRuntimeCompilation();
#endif
...
I did not include the if (Env.IsDevelopment())
as I did not want to deal with the global Env injection/variable. Not really needed for what I am doing.
I can confirm with the settings above, might be slightly different for various setups, MVC/Pages/Blazor but overall the Publish - Configuration: Release, now does NOT include all those debug files in the published file output.
I still think there has to be an easier way down the road but for now, this does work.
Thank you!
Most helpful comment
I suspect this feature is working as designed. Roslyn assemblies (those prefixed with Microsoft.CodeAnalysis.*) were removed from the shared framework as of 3.0. Those assemblies are needed to support runtime view compilation.
Have you already tried adding a
Condition
to your package ref? For example:Then in your
Startup
class, you could do something like the following:/cc: @pranavkm