Skip to content

Commit 191feab

Browse files
authored
Fix DI scoping violation in RazorSourceGeneratorTestsBase (#12432)
2 parents 47fe376 + d90a602 commit 191feab

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTestsBase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,13 @@ protected static async Task<string> RenderRazorPageAsync(Compilation compilation
150150
}
151151
});
152152
var app = appBuilder.Build();
153+
154+
// Create a service scope to properly handle scoped services like IViewBufferScope.
155+
// ASP.NET Core's DI validation prevents resolving scoped services from the root provider.
156+
using var scope = app.Services.CreateScope();
153157
var httpContext = new DefaultHttpContext
154158
{
155-
RequestServices = app.Services
159+
RequestServices = scope.ServiceProvider
156160
};
157161
var requestFeature = new HttpRequestFeature
158162
{
@@ -184,7 +188,7 @@ protected static async Task<string> RenderRazorPageAsync(Compilation compilation
184188
.ToImmutableArray();
185189

186190
// Render the page.
187-
var view = ActivatorUtilities.CreateInstance<RazorView>(app.Services,
191+
var view = ActivatorUtilities.CreateInstance<RazorView>(scope.ServiceProvider,
188192
/* IReadOnlyList<IRazorPage> viewStartPages */ viewStarts,
189193
/* IRazorPage razorPage */ page);
190194
await view.RenderAsync(viewContext);

0 commit comments

Comments
 (0)