Add runtime mode (BackofficeDevelopment, Development and Production) (#12631)

* Add runtime mode setting

* Only enable Razor runtime compilation in development modes

* Only enable ModelsBuilder generation in development modes

* Fix disabling ModelsBuilder controllers

* Add IRuntimeModeValidationService and IRuntimeModeValidator

* Add JITOptimizerValidator

* Add UmbracoApplicationUrlValidator

* Add UseHttpsValidator

* Add RuntimeMinificationValidator

* Add ModelsBuilderModeValidator

* Remove .NET 6 preview 1 fix for Razor runtime compilation

* Only allow InMemoryAuto in backoffice development mode

* Make runtime mode validators public, so they can be easily removed if required

* Add comment to highlight removing RazorCompileOnBuild, RazorCompileOnPublish and CopyRazorGenerateFilesToPublishDirectory when using ModelsMode InMemoryAuto

* Add documentation

* Update src/Umbraco.Web.Common/ModelsBuilder/NoopModelsBuilderDashboardProvider.cs

Co-authored-by: Ronald Barendse <ronald@barend.se>

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
This commit is contained in:
Ronald Barendse
2022-07-01 08:48:05 +02:00
committed by GitHub
parent 7d545a7dfc
commit dca4d0f167
25 changed files with 791 additions and 420 deletions

View File

@@ -224,13 +224,12 @@ public static partial class UmbracoBuilderExtensions
{
// TODO: We need to figure out if we can work around this because calling AddControllersWithViews modifies the global app and order is very important
// this will directly affect developers who need to call that themselves.
// We need to have runtime compilation of views when using umbraco. We could consider having only this when a specific config is set.
// But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay.
IMvcBuilder mvcBuilder = builder.Services
.AddControllersWithViews();
IMvcBuilder mvcBuilder = builder.Services.AddControllersWithViews();
FixForDotnet6Preview1(builder.Services);
mvcBuilder.AddRazorRuntimeCompilation();
if (builder.Config.GetRuntimeMode() != RuntimeMode.Production)
{
mvcBuilder.AddRazorRuntimeCompilation();
}
mvcBuilding?.Invoke(mvcBuilder);
@@ -421,30 +420,4 @@ public static partial class UmbracoBuilderExtensions
wrappedWebRoutingSettings,
webHostEnvironment);
}
/// <summary>
/// This fixes an issue for .NET6 Preview1, that in AddRazorRuntimeCompilation cannot remove the existing
/// IViewCompilerProvider.
/// </summary>
/// <remarks>
/// When running .NET6 Preview1 there is an issue with looks to be fixed when running ASP.NET Core 6.
/// This issue is because the default implementation of IViewCompilerProvider has changed, so the
/// AddRazorRuntimeCompilation extension can't remove the default and replace with the runtimeviewcompiler.
/// This method basically does the same as the ASP.NET Core 6 version of AddRazorRuntimeCompilation
/// https://github.com/dotnet/aspnetcore/blob/f7dc5e24af7f9692a1db66741954b90b42d84c3a/src/Mvc/Mvc.Razor.RuntimeCompilation/src/DependencyInjection/RazorRuntimeCompilationMvcCoreBuilderExtensions.cs#L71-L80
/// While running .NET5 this does nothing as the ImplementationType has another FullName, and this is handled by the
/// .NET5 version of AddRazorRuntimeCompilation
/// </remarks>
private static void FixForDotnet6Preview1(IServiceCollection services)
{
ServiceDescriptor? compilerProvider = services.FirstOrDefault(f =>
f.ServiceType == typeof(IViewCompilerProvider) &&
f.ImplementationType?.Assembly == typeof(IViewCompilerProvider).Assembly &&
f.ImplementationType.FullName == "Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultViewCompiler");
if (compilerProvider != null)
{
services.Remove(compilerProvider);
}
}
}