Skip to content

Commit

Permalink
Validate scopes when building service providers in tests (#25438)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Aug 6, 2021
1 parent 2053a66 commit 018b50b
Show file tree
Hide file tree
Showing 66 changed files with 298 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public ConfigurationPatternsTest(CrossStoreFixture fixture)
[ConditionalFact]
public void Can_register_multiple_context_types()
{
var serviceProvider = new ServiceCollection()
using var scope = new ServiceCollection()
.AddDbContext<MultipleContext1>()
.AddDbContext<MultipleContext2>()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true)
.CreateScope();

var serviceProvider = scope.ServiceProvider;

using (var context = serviceProvider.GetRequiredService<MultipleContext1>())
{
Assert.True(context.SimpleEntities.Any());
Expand Down Expand Up @@ -102,7 +105,7 @@ var serviceProvider
= new ServiceCollection()
.AddScoped<SomeService>()
.AddDbContext<MultipleProvidersContext>()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

MultipleProvidersContext context1;
MultipleProvidersContext context2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Throws_with_new_when_non_relational_provider_in_use()
.UseInternalServiceProvider(
new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider())
.BuildServiceProvider(validateScopes: true))
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;

Expand All @@ -34,7 +34,7 @@ public void Throws_with_add_when_non_relational_provider_in_use()
.AddEntityFrameworkInMemoryDatabase()
.AddDbContext<ConstructorTestContext1A>(
(p, b) => b.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseInternalServiceProvider(p))
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

using var serviceScope = appServiceProvider
.GetRequiredService<IServiceScopeFactory>()
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Design.Tests/Design/DesignTimeServicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private ServiceProvider CreateDesignServiceProvider(
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
: servicesBuilder
.CreateServiceCollection(context))
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);
}

private Assembly Compile(string assemblyCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static TestWebHost BuildWebHost(string[] args)
.UseInMemoryDatabase("In-memory test database")
.EnableServiceProviderCaching(false)
.Options))
.BuildServiceProvider());
.BuildServiceProvider(validateScopes: true));
}

private static class TestProgramWithContextFactory
Expand All @@ -139,7 +139,7 @@ private static TestWebHost BuildWebHost(string[] args)
=> new(
new ServiceCollection()
.AddDbContextFactory<TestContextFromFactory>(b => b.UseInMemoryDatabase("In-memory test database"))
.BuildServiceProvider());
.BuildServiceProvider(validateScopes: true));
}

private static class TestProgramRelational
Expand Down Expand Up @@ -175,7 +175,7 @@ private static TestWebHost CreateWebHost(Func<DbContextOptionsBuilder, DbContext
.AddDbContext<TestContext>(
b =>
configureProvider(b.EnableServiceProviderCaching(false)))
.BuildServiceProvider());
.BuildServiceProvider(validateScopes: true));

private class TestContext : DbContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ private static IMigrationsCodeGenerator CreateMigrationsCodeGenerator()
var reporter = new TestOperationReporter();
return new DesignTimeServicesBuilder(testAssembly, testAssembly, reporter, new string[0])
.CreateServiceCollection(SqlServerTestHelpers.Instance.CreateContext())
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IMigrationsCodeGenerator>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void Required_options_to_GenerateModel_are_not_null()
{
var generator = CreateServices()
.AddSingleton<IProviderCodeGeneratorPlugin, TestCodeGeneratorPlugin>()
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IModelCodeGenerator>();

Assert.StartsWith(
Expand Down Expand Up @@ -234,7 +234,7 @@ public void Plugins_work()
{
var generator = CreateServices()
.AddSingleton<IProviderCodeGeneratorPlugin, TestCodeGeneratorPlugin>()
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IModelCodeGenerator>();

var scaffoldedModel = generator.GenerateModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static IModelCodeGenerator CreateGenerator()
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>()
.AddSingleton<IProviderConfigurationCodeGenerator, TestProviderCodeGenerator>()
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IModelCodeGenerator>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3216,7 +3216,7 @@ protected void Test(
options.ContextType = context.GetType();

var generator = services
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<ICompiledModelCodeGeneratorSelector>()
.Select(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void Test(
var services = CreateServices();
AddScaffoldingServices(services);

var generator = services.BuildServiceProvider()
var generator = services.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IModelCodeGenerator>();

options.ModelNamespace ??= "TestNamespace";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public RelationalDatabaseModelFactoryTest()
_factory = new DesignTimeServicesBuilder(assembly, assembly, _reporter, new string[0])
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.AddSingleton<IScaffoldingModelFactory, FakeScaffoldingModelFactory>()
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IScaffoldingModelFactory>();

_reporter.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using System.IO;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -135,7 +133,7 @@ private static IReverseEngineerScaffolder CreateScaffolder()
new TestOperationReporter(),
new string[0])
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.BuildServiceProvider()
.BuildServiceProvider() // No scope validation; design services only resolved once
.GetRequiredService<IReverseEngineerScaffolder>();

[ConditionalFact]
Expand All @@ -151,7 +149,7 @@ public void ScaffoldModel_works_with_named_connection_string()
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.AddSingleton<INamedConnectionStringResolver>(resolver)
.AddSingleton<IDatabaseModelFactory>(databaseModelFactory)
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IReverseEngineerScaffolder>();

var result = scaffolder.ScaffoldModel(
Expand Down Expand Up @@ -181,7 +179,7 @@ public void ScaffoldModel_works_with_overridden_connection_string()
.CreateServiceCollection("Microsoft.EntityFrameworkCore.SqlServer")
.AddSingleton<INamedConnectionStringResolver>(resolver)
.AddSingleton<IDatabaseModelFactory>(databaseModelFactory)
.BuildServiceProvider()
.BuildServiceProvider(validateScopes: true)
.GetRequiredService<IReverseEngineerScaffolder>();

var result = scaffolder.ScaffoldModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task Can_use_two_non_generated_integers_as_composite_key_end_to_end
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

var ticks = DateTime.UtcNow.Ticks;

Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task Can_use_generated_values_in_composite_key_end_to_end()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

long id1;
var id2 = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task Only_one_part_of_a_composite_key_needs_to_vary_for_uniqueness(
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

var ids = new int[3];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Can_save_and_query_with_explicit_services_and_OnConfiguring()
{
var services = new ServiceCollection();
services.AddEntityFrameworkInMemoryDatabase();
var serviceProvider = services.BuildServiceProvider();
var serviceProvider = services.BuildServiceProvider(validateScopes: true);

using (var context = new ExplicitServicesImplicitConfigBlogContext(serviceProvider))
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Can_save_and_query_with_explicit_services_and_explicit_config()
.UseInMemoryDatabase(nameof(ExplicitServicesAndConfigBlogContext))
.UseInternalServiceProvider(
new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase().BuildServiceProvider());
.AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(validateScopes: true));

using (var context = new ExplicitServicesAndConfigBlogContext(optionsBuilder.Options))
{
Expand Down Expand Up @@ -195,7 +195,7 @@ public void Throws_on_attempt_to_use_store_with_no_store_services()
{
var serviceCollection = new ServiceCollection();
new EntityFrameworkServicesBuilder(serviceCollection).TryAddCoreServices();
var serviceProvider = serviceCollection.BuildServiceProvider();
var serviceProvider = serviceCollection.BuildServiceProvider(validateScopes: true);

Assert.Equal(
CoreStrings.NoProviderConfigured,
Expand Down Expand Up @@ -234,7 +234,7 @@ public void Can_register_context_with_DI_container_and_have_it_injected()
.AddTransient<InjectContextController>()
.AddEntityFrameworkInMemoryDatabase();

var serviceProvider = services.BuildServiceProvider();
var serviceProvider = services.BuildServiceProvider(validateScopes: true);

serviceProvider.GetRequiredService<InjectContextController>().Test();
}
Expand Down Expand Up @@ -292,7 +292,7 @@ public void Can_register_context_and_configuration_with_DI_container_and_have_bo
.AddTransient<InjectContextAndConfigurationController>()
.AddSingleton(p => optionsBuilder.UseInternalServiceProvider(p).Options)
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

serviceProvider.GetRequiredService<InjectContextAndConfigurationController>().Test();
}
Expand Down Expand Up @@ -346,7 +346,7 @@ public void Can_register_configuration_with_DI_container_and_have_it_injected()
.AddSingleton(optionsBuilder.Options)
.AddEntityFrameworkInMemoryDatabase();

var serviceProvider = services.BuildServiceProvider();
var serviceProvider = services.BuildServiceProvider(validateScopes: true);

serviceProvider.GetRequiredService<InjectConfigurationController>().Test();
}
Expand Down Expand Up @@ -406,7 +406,7 @@ public void Can_inject_different_configurations_into_different_contexts()
.AddSingleton(p => blogOptions.UseInternalServiceProvider(p).Options)
.AddSingleton(p => accountOptions.UseInternalServiceProvider(p).Options)
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

serviceProvider.GetRequiredService<InjectDifferentConfigurationsBlogController>().Test();
serviceProvider.GetRequiredService<InjectDifferentConfigurationsAccountController>().Test();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static readonly IServiceProvider _serviceProvider
= new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.AddScoped<IValueGeneratorSelector, CustomInMemoryValueGeneratorSelector>()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
Expand Down Expand Up @@ -90,7 +90,7 @@ private class CustomValueGeneratorContextAnnotateType : DbContext
private static readonly IServiceProvider _serviceProvider
= new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
Expand Down Expand Up @@ -132,7 +132,7 @@ private class CustomValueGeneratorContextAnnotateFactory : DbContext
private static readonly IServiceProvider _serviceProvider
= new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private async Task SaveChanges_logs_DatabaseErrorLogState_test(bool async)
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.AddSingleton<ILoggerFactory>(loggerFactory)
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

using var context = new BloggingContext(serviceProvider);
context.Blogs.Add(
Expand Down Expand Up @@ -104,7 +104,7 @@ private async Task Query_logs_DatabaseErrorLogState_test(Func<BloggingContext, T
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.AddSingleton<ILoggerFactory>(loggerFactory)
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

using var context = new BloggingContext(serviceProvider);
context.Blogs.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Can_add_update_delete_end_to_end()
.AddEntityFrameworkInMemoryDatabase()
.AddSingleton<ILoggerFactory>(new ListLoggerFactory())
.AddSingleton(TestModelSource.GetFactory(OnModelCreating))
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

var options = new DbContextOptionsBuilder()
.UseInternalServiceProvider(serviceProvider)
Expand Down
6 changes: 3 additions & 3 deletions test/EFCore.InMemory.FunctionalTests/GlobalDatabaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void AddDbContext_does_not_force_different_internal_service_provider()
var serviceProvider = new ServiceCollection()
.AddDbContext<BooFooContext>(
b => b.UseInMemoryDatabase(nameof(BooFooContext)))
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

using var scope = serviceProvider.CreateScope();
{
Expand Down Expand Up @@ -141,7 +141,7 @@ public void Global_store_can_be_used_when_AddDbContext_force_different_internal_
b =>
b.UseInMemoryDatabase(nameof(BooFooContext), _databaseRoot)
.EnableServiceProviderCaching(false))
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

using var scope = serviceProvider.CreateScope();
{
Expand Down Expand Up @@ -173,7 +173,7 @@ private class ChangeSdlCacheContext : DbContext
private static readonly IServiceProvider _serviceProvider
= new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

private readonly bool _on;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task Can_use_GUIDs_end_to_end_async()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

var guids = new List<Guid>();
var guidsHash = new HashSet<Guid>();
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.InMemory.FunctionalTests/InMemoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public static ServiceProvider BuildServiceProvider(IServiceCollection providerSe
=> InMemoryTestStoreFactory.Instance.AddProviderServices(
providerServices
?? new ServiceCollection())
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Can_use_sequence_end_to_end()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

AddEntities(serviceProvider);
AddEntities(serviceProvider);
Expand Down Expand Up @@ -51,7 +51,7 @@ public async Task Can_use_sequence_end_to_end_async()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

await AddEntitiesAsync(serviceProvider);
await AddEntitiesAsync(serviceProvider);
Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task Can_use_sequence_end_to_end_from_multiple_contexts_concurrentl
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.BuildServiceProvider(validateScopes: true);

const int threadCount = 50;

Expand Down
Loading

0 comments on commit 018b50b

Please sign in to comment.