Fix DI scoping violation in RazorSourceGeneratorTestsBase #12432
+6
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The test infrastructure assigns the root service provider to
HttpContext.RequestServices, causingInvalidOperationExceptionwhen resolving scoped services likeIViewBufferScopein environments with strict DI validation (.NET 8+). This fails 66/166 tests on Ubuntu 24.04 with .NET 8.0.21.Changes
app.Services.CreateScope()and assign toRequestServicesActivatorUtilities.CreateInstance<RazorView>()usingstatementAligns with ASP.NET Core DI lifetime semantics.
Original prompt
This section details on the original issue you should resolve
<issue_title>DI Scoping Bug in RazorSourceGeneratorTestsBase Causes Test Failures</issue_title>
<issue_description># DI Scoping Bug in RazorSourceGeneratorTestsBase Causes Test Failures
Summary
The
Microsoft.NET.Sdk.Razor.SourceGenerators.Testtest suite fails with 66 out of 166 tests (40%) throwingInvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers.IViewBufferScope' from root providerdue to improper dependency injection scoping in the test infrastructure.Environment
Steps to Reproduce
./restore.sh./build.sh -testMicrosoft.NET.Sdk.Razor.SourceGenerators.TestExpected Behavior
All 166 tests should pass.
Actual Behavior
66 tests fail with:
Example Stack Trace
Test Results
Before fix:
After fix:
Root Cause
In
src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTestsBase.csat line 155, the test code assigns the root service provider toHttpContext.RequestServices:When
RazorView.RenderAsync()executes (line 190), it attempts to resolveIViewBufferScope, which is registered as a scoped service in ASP.NET Core MVC. ASP.NET Core's dependency injection validation (enabled in .NET 8+) correctly prevents resolving scoped services from the root provider.Solution
Create a service scope before assigning to
RequestServices:Fix
File:
src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTestsBase.csLine: 155
Before:
After:
This one-line change fixes all 66 failing tests by properly adhering to ASP.NET Core's DI scoping rules.
Additional Context
Impact
References
</issue_description>
Comments on the Issue (you are @copilot in this section)
@davidwengier This one I've never seen fail in any local run or CI, that I can recall. Curious to know more about the process for creating these issues @StevenTCramer if you can share? @davidwengier Interesting. I wonder why this one doesn't fail for us? There are certainly differences between WSL and a "real" Ubuntu machine we do CI on, but I wouldn't have thought any of them would have had any effect on this.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.