Skip to content

Commit f2df363

Browse files
committed
Fix for issue while running .NET6 with runtime compiled views
1 parent fee3d07 commit f2df363

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Hosting;
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.AspNetCore.Mvc.ApplicationModels;
12+
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
1213
using Microsoft.AspNetCore.Server.Kestrel.Core;
1314
using Microsoft.Extensions.Configuration;
1415
using Microsoft.Extensions.DependencyInjection;
@@ -221,14 +222,42 @@ public static IUmbracoBuilder AddMvcAndRazor(this IUmbracoBuilder builder, Actio
221222
// We need to have runtime compilation of views when using umbraco. We could consider having only this when a specific config is set.
222223
// But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay.
223224
IMvcBuilder mvcBuilder = builder.Services
224-
.AddControllersWithViews()
225-
.AddRazorRuntimeCompilation();
225+
.AddControllersWithViews();
226+
227+
FixForDotnet6Preview1(builder.Services);
228+
mvcBuilder.AddRazorRuntimeCompilation();
226229

227230
mvcBuilding?.Invoke(mvcBuilder);
228231

229232
return builder;
230233
}
231234

235+
/// <summary>
236+
/// This fixes an issue for .NET6 Preview1, that in AddRazorRuntimeCompilation cannot remove the existing IViewCompilerProvider.
237+
/// </summary>
238+
/// <remarks>
239+
/// When running .NET6 Preview1 there is an issue with looks to be fixed when running ASP.NET Core 6.
240+
/// This issue is because the default implementation of IViewCompilerProvider has changed, so the
241+
/// AddRazorRuntimeCompilation extension can't remove the default and replace with the runtimeviewcompiler.
242+
///
243+
/// This method basically does the same as the ASP.NET Core 6 version of AddRazorRuntimeCompilation
244+
/// https://github.com/dotnet/aspnetcore/blob/f7dc5e24af7f9692a1db66741954b90b42d84c3a/src/Mvc/Mvc.Razor.RuntimeCompilation/src/DependencyInjection/RazorRuntimeCompilationMvcCoreBuilderExtensions.cs#L71-L80
245+
///
246+
/// While running .NET5 this does nothing as the ImplementationType has another FullName, and this is handled by the .NET5 version of AddRazorRuntimeCompilation
247+
/// </remarks>
248+
private static void FixForDotnet6Preview1(IServiceCollection services)
249+
{
250+
var compilerProvider = services.FirstOrDefault(f =>
251+
f.ServiceType == typeof(IViewCompilerProvider) &&
252+
f.ImplementationType?.Assembly == typeof(IViewCompilerProvider).Assembly &&
253+
f.ImplementationType.FullName == "Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultViewCompiler");
254+
255+
if (compilerProvider != null)
256+
{
257+
services.Remove(compilerProvider);
258+
}
259+
}
260+
232261
/// <summary>
233262
/// Add runtime minifier support for Umbraco
234263
/// </summary>

0 commit comments

Comments
 (0)