diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs index cc322ce1f8..de1a727273 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs @@ -97,6 +97,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor throw new ArgumentNullException(nameof(context)); } + // This GetRequiredService call is by design. ViewBufferScope is a scoped service, RazorViewEngine + // is the component responsible for creating RazorViews and it is a Singleton service. It doesn't + // have access to the RequestServices so requiring the service when we render the page is the best + // we can do. _bufferScope = context.HttpContext.RequestServices.GetRequiredService(); var bodyWriter = await RenderPageAsync(RazorPage, context, invokeViewStarts: true); await RenderLayoutAsync(context, bodyWriter);