Conversation
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsThis is related to dotnet/efcore#31178.
|
| DependencyInjectionEventSource.Log.ServiceResolved(this, serviceIdentifier.ServiceType); | ||
| return value; | ||
| }; | ||
| return new ServiceAccessor { CallSite = callSite, RealizedService = scope => value }; |
There was a problem hiding this comment.
So the issue was that the CallSite was not captured previously?
There was a problem hiding this comment.
Unfortunately, I don't quite understand why this caused the error. At the moment I can't write a unit test that reproduces the crash. I just rolled back the logic as it was and tried it on ef core tests. The tests were green, at least on my machine. More research needed
There was a problem hiding this comment.
I have a local repro. I'll update this PR with that soon.
There was a problem hiding this comment.
I wasn't able to push to your branch - see the comments on the PR for the test.
The issue is related to swapping to an emit-based version of the service accessor which doesn't have the logic that was added to the previous lambda.
|
@mapogolions thanks for jumping on this; I was also working locally on a fix and about to reach out to you. Please add a test that verifies the EF issue before\after - LMK if you want me to assist. |
| public bool IsScope => !_serviceProvider.Root.IsRootScope; | ||
| } | ||
|
|
||
| private class ServiceAccessor |
There was a problem hiding this comment.
This should be sealed.
|
@mapogolions here's a repro that is related to the EF issue - please add this test to [Fact]
public async void GetService_Throws_WhenGetServiceForScopedServiceIsCalledOnRoot_IL_Replacement()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IBar, Bar>();
var serviceProvider = serviceCollection.BuildServiceProvider(validateScopes: true);
// Act + Assert
using (var scope = serviceProvider.CreateScope())
{
// Switch to an emit-based version which is triggered in the background after 2 calls to GetService.
scope.ServiceProvider.GetRequiredService(typeof(IBar));
scope.ServiceProvider.GetRequiredService(typeof(IBar));
// Give the background thread time to generate the emit version.
await Task.Delay(100);
// Ensure the emit-based version has the correct scope checks.
var exception = Assert.Throws<InvalidOperationException>(serviceProvider.GetRequiredService<IBar>);
Assert.Equal($"Cannot resolve scoped service '{typeof(IBar)}' from root provider.", exception.Message);
}
} |
|
Thanks again @mapogolions |
@steveharter
This is related to dotnet/efcore#31178 and #89109
Reworked the logic a bit and made it look like what it was before this PR. This helps pass failed tests of efcore. Could you please check it