Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register concrete DbContext type in D.I. even when registration for interface is also made #25537

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,16 @@ public static IServiceCollection AddDbContextPool<TContextService, TContextImple
AddPoolingOptions<TContextImplementation>(serviceCollection, optionsAction, poolSize);

serviceCollection.TryAddSingleton<IDbContextPool<TContextImplementation>, DbContextPool<TContextImplementation>>();
serviceCollection.AddScoped<IScopedDbContextLease<TContextImplementation>, ScopedDbContextLease<TContextImplementation>>();
serviceCollection.TryAddScoped<IScopedDbContextLease<TContextImplementation>, ScopedDbContextLease<TContextImplementation>>();

serviceCollection.AddScoped<TContextService>(
serviceCollection.TryAddScoped<TContextService>(
sp => sp.GetRequiredService<IScopedDbContextLease<TContextImplementation>>().Context);

if (typeof(TContextService) != typeof(TContextImplementation))
{
serviceCollection.TryAddScoped(p => (TContextImplementation)p.GetService<TContextService>()!);
}

return serviceCollection;
}

Expand Down Expand Up @@ -526,16 +531,21 @@ public static IServiceCollection AddDbContext<TContextService, TContextImplement
if (serviceCollection.Any(d => d.ServiceType == typeof(IDbContextFactorySource<TContextImplementation>)))
{
// Override registration made by AddDbContextFactory
var serviceDescriptor = serviceCollection.FirstOrDefault(d => d.ServiceType == typeof(TContextService));
var serviceDescriptor = serviceCollection.FirstOrDefault(d => d.ServiceType == typeof(TContextImplementation));
if (serviceDescriptor != null)
{
serviceCollection.Remove(serviceDescriptor);
serviceCollection.Add(new ServiceDescriptor(typeof(TContextService), typeof(TContextImplementation), contextLifetime));
}
}
else

serviceCollection.TryAdd(new ServiceDescriptor(typeof(TContextService), typeof(TContextImplementation), contextLifetime));

if (typeof(TContextService) != typeof(TContextImplementation))
{
serviceCollection.TryAdd(new ServiceDescriptor(typeof(TContextService), typeof(TContextImplementation), contextLifetime));
serviceCollection.TryAdd(
new ServiceDescriptor(typeof(TContextImplementation),
p => (TContextImplementation)p.GetService<TContextService>()!,
contextLifetime));
}

return serviceCollection;
Expand Down
12 changes: 12 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/DbContextPoolingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ public void Validate_pool_size_with_service_interface_default()
.FindExtension<CoreOptionsExtension>()!.MaxPoolSize);
}

[ConditionalFact]
public void Pool_can_get_context_by_concrete_type_even_when_service_interface_is_used()
{
var serviceProvider = BuildServiceProvider<IPooledContext, PooledContext>();

using var scope = serviceProvider.CreateScope();

Assert.Same(
scope.ServiceProvider.GetRequiredService<IPooledContext>(),
scope.ServiceProvider.GetRequiredService<PooledContext>());
}

[ConditionalFact]
public void Validate_pool_size_with_factory_default()
{
Expand Down
5 changes: 5 additions & 0 deletions test/EFCore.Tests/DbContextServicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,11 @@ public void Can_add_derived_context(bool useInterface)
? (ConstructorTestContextWithOC1A)serviceScope.ServiceProvider.GetService<IConstructorTestContextWithOC1A>()
: serviceScope.ServiceProvider.GetService<ConstructorTestContextWithOC1A>();

if (useInterface)
{
Assert.Same(context1, serviceScope.ServiceProvider.GetService<ConstructorTestContextWithOC1A>());
}

Assert.NotNull(singleton[0] = context1.GetService<IInMemoryStoreCache>());
Assert.NotNull(singleton[1] = context1.GetService<ILoggerFactory>());
Assert.NotNull(singleton[2] = context1.GetService<IMemoryCache>());
Expand Down