From ed8c74404105e65745fb6febed174dd10cda3664 Mon Sep 17 00:00:00 2001 From: jacalvar Date: Wed, 25 May 2016 12:57:35 -0700 Subject: [PATCH] [Fixes #4218] Adds a comment justifying the use of service locator in this case --- src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs | 4 ++++ 1 file changed, 4 insertions(+) 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);