Skip to content

Commit

Permalink
Register concrete DbContext type in D.I. even when registration for i…
Browse files Browse the repository at this point in the history
…nterface is also made (#25537)

Fixes #14307

This means that tooling (such as the database middleware) can still find the context, while the application can resolve using the interface.
  • Loading branch information
ajcvickers committed Aug 17, 2021
1 parent ab0feee commit b603d37
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,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 @@ -592,16 +597,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

0 comments on commit b603d37

Please sign in to comment.