Skip to content

Commit 8cdc34f

Browse files
Copilotdavidwengier
andcommitted
Fix DI scoping issue in RazorSourceGeneratorTestsBase
- Create scoped service provider instead of using root provider - Update both RequestServices and ActivatorUtilities.CreateInstance to use scoped provider - Properly dispose scope with using statement This fixes test failures in environments with stricter DI validation and aligns with ASP.NET Core best practices. Co-authored-by: davidwengier <[email protected]>
1 parent c213261 commit 8cdc34f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ protected static async Task<string> RenderRazorPageAsync(Compilation compilation
150150
}
151151
});
152152
var app = appBuilder.Build();
153+
154+
using var scope = app.Services.CreateScope();
153155
var httpContext = new DefaultHttpContext
154156
{
155-
RequestServices = app.Services
157+
RequestServices = scope.ServiceProvider
156158
};
157159
var requestFeature = new HttpRequestFeature
158160
{
@@ -184,7 +186,7 @@ protected static async Task<string> RenderRazorPageAsync(Compilation compilation
184186
.ToImmutableArray();
185187

186188
// Render the page.
187-
var view = ActivatorUtilities.CreateInstance<RazorView>(app.Services,
189+
var view = ActivatorUtilities.CreateInstance<RazorView>(scope.ServiceProvider,
188190
/* IReadOnlyList<IRazorPage> viewStartPages */ viewStarts,
189191
/* IRazorPage razorPage */ page);
190192
await view.RenderAsync(viewContext);

0 commit comments

Comments
 (0)