From 051928f3245f34a61248cd3ed21ab820a9cc889c Mon Sep 17 00:00:00 2001 From: Thorsten Thiel Date: Sun, 22 Oct 2023 14:18:33 +0200 Subject: [PATCH] Fix DI lifetimes --- .../Fluss.HotChocolate.csproj | 2 +- src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj | 2 +- .../ServiceCollectionExtensions.cs | 37 ++++++------------- src/Fluss.Testing/Fluss.Testing.csproj | 2 +- src/Fluss.UnitTest/Fluss.UnitTest.csproj | 2 +- .../ServiceCollectionExtensions.cs | 6 +-- src/Fluss/Fluss.csproj | 2 +- src/Fluss/ServiceCollectionExtensions.cs | 2 +- .../SideEffectsServiceCollectionExtension.cs | 2 +- .../ValidationServiceCollectionExtension.cs | 4 +- 10 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/Fluss.HotChocolate/Fluss.HotChocolate.csproj b/src/Fluss.HotChocolate/Fluss.HotChocolate.csproj index fb5cc40..df513f9 100644 --- a/src/Fluss.HotChocolate/Fluss.HotChocolate.csproj +++ b/src/Fluss.HotChocolate/Fluss.HotChocolate.csproj @@ -1,7 +1,7 @@  - net8.0; net7.0; net6.0 + net8.0; net7.0 enable enable diff --git a/src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj b/src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj index 18760ab..88633ac 100644 --- a/src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj +++ b/src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj @@ -1,7 +1,7 @@  - net8.0; net7.0; net6.0 + net8.0; net7.0 enable enable diff --git a/src/Fluss.PostgreSQL/ServiceCollectionExtensions.cs b/src/Fluss.PostgreSQL/ServiceCollectionExtensions.cs index 81364c4..f2d2784 100644 --- a/src/Fluss.PostgreSQL/ServiceCollectionExtensions.cs +++ b/src/Fluss.PostgreSQL/ServiceCollectionExtensions.cs @@ -14,10 +14,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddPostgresEventSourcingRepository(this IServiceCollection services, string connectionString, Assembly? upcasterSourceAssembly = null) { - if (services is null) - { - throw new ArgumentNullException(nameof(services)); - } + ArgumentNullException.ThrowIfNull(services); if (upcasterSourceAssembly is not null) { @@ -27,7 +24,7 @@ public static IServiceCollection AddPostgresEventSourcingRepository(this IServic } return services - .AddScoped() + .AddBaseEventRepository() .AddFluentMigratorCore() .ConfigureRunner(rb => rb .AddPostgres() @@ -43,15 +40,15 @@ public static IServiceCollection AddPostgresEventSourcingRepository(this IServic public class Migrator : BackgroundService { private readonly ILogger _logger; - private readonly IMigrationRunner _migrationRunner; + private readonly IServiceProvider _serviceProvider; private bool _didFinish; private readonly SemaphoreSlim _didFinishChanged = new(0, 1); - public Migrator(IMigrationRunner migrationRunner, ILogger logger) + public Migrator(ILogger logger, IServiceProvider serviceProvider) { - _migrationRunner = migrationRunner; _logger = logger; + _serviceProvider = serviceProvider; } public async Task WaitForFinish() @@ -77,30 +74,20 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { try { - Migrate(); + var scope = _serviceProvider.CreateScope(); + var migrationRunner = scope.ServiceProvider.GetRequiredService() + migrationRunner.MigrateUp(); + + _didFinish = true; + _didFinishChanged.Release(); } catch (Exception e) { _logger.LogError(e, "Error while migrating"); + Environment.Exit(-1); } }, stoppingToken); } - - public void Migrate() - { - //_migrationRunner.ListMigrations(); - try - { - _migrationRunner.MigrateUp(); - } - catch - { - Environment.Exit(-1); - } - - _didFinish = true; - _didFinishChanged.Release(); - } } public class Upcaster : BackgroundService diff --git a/src/Fluss.Testing/Fluss.Testing.csproj b/src/Fluss.Testing/Fluss.Testing.csproj index 69647b0..d3eab38 100644 --- a/src/Fluss.Testing/Fluss.Testing.csproj +++ b/src/Fluss.Testing/Fluss.Testing.csproj @@ -1,7 +1,7 @@  - net8.0; net7.0; net6.0 + net8.0; net7.0 enable enable diff --git a/src/Fluss.UnitTest/Fluss.UnitTest.csproj b/src/Fluss.UnitTest/Fluss.UnitTest.csproj index 12e7d85..0e81098 100644 --- a/src/Fluss.UnitTest/Fluss.UnitTest.csproj +++ b/src/Fluss.UnitTest/Fluss.UnitTest.csproj @@ -1,7 +1,7 @@ - net8.0; net7.0; net6.0 + net8.0; net7.0 enable enable diff --git a/src/Fluss/Authentication/ServiceCollectionExtensions.cs b/src/Fluss/Authentication/ServiceCollectionExtensions.cs index 05f2360..32dd6ad 100644 --- a/src/Fluss/Authentication/ServiceCollectionExtensions.cs +++ b/src/Fluss/Authentication/ServiceCollectionExtensions.cs @@ -11,9 +11,9 @@ public static IServiceCollection AddPoliciesFrom(this IServiceCollection service foreach (var policyType in policyTypes) { - services = services - .AddScoped(policyType) - .AddScoped(sp => (Policy)sp.GetRequiredService(policyType)); + services + .AddSingleton(policyType) + .AddSingleton(sp => (Policy)sp.GetRequiredService(policyType)); } return services; diff --git a/src/Fluss/Fluss.csproj b/src/Fluss/Fluss.csproj index 9a0b67f..09d03d5 100644 --- a/src/Fluss/Fluss.csproj +++ b/src/Fluss/Fluss.csproj @@ -1,7 +1,7 @@  - net8.0; net7.0; net6.0 + net8.0; net7.0 enable enable Fluss diff --git a/src/Fluss/ServiceCollectionExtensions.cs b/src/Fluss/ServiceCollectionExtensions.cs index 9f318b6..54a38be 100644 --- a/src/Fluss/ServiceCollectionExtensions.cs +++ b/src/Fluss/ServiceCollectionExtensions.cs @@ -91,7 +91,7 @@ public static IServiceCollection AddUpcasters(this IServiceCollection services, foreach (var upcaster in upcasters) { - services.AddScoped(upcasterType, upcaster); + services.AddSingleton(upcasterType, upcaster); } return services.AddSingleton().AddSingleton(); diff --git a/src/Fluss/SideEffects/SideEffectsServiceCollectionExtension.cs b/src/Fluss/SideEffects/SideEffectsServiceCollectionExtension.cs index 40906a2..f1617ec 100644 --- a/src/Fluss/SideEffects/SideEffectsServiceCollectionExtension.cs +++ b/src/Fluss/SideEffects/SideEffectsServiceCollectionExtension.cs @@ -11,7 +11,7 @@ public static IServiceCollection RegisterSideEffects(this IServiceCollection ser foreach (var @class in implementingClasses) { - services.AddScoped(typeof(SideEffect), @class); + services.AddSingleton(typeof(SideEffect), @class); } services.AddHostedService(); diff --git a/src/Fluss/Validation/ValidationServiceCollectionExtension.cs b/src/Fluss/Validation/ValidationServiceCollectionExtension.cs index 0dfced7..c7ba6ce 100644 --- a/src/Fluss/Validation/ValidationServiceCollectionExtension.cs +++ b/src/Fluss/Validation/ValidationServiceCollectionExtension.cs @@ -12,7 +12,7 @@ public static IServiceCollection AddValidationFrom(this IServiceCollection servi sourceAssembly.GetTypes().Where(t => t.IsAssignableTo(aggregateValidatorType)).ToList(); foreach (var aggregateValidator in aggregateValidators) { - services.AddScoped(aggregateValidatorType, aggregateValidator); + services.AddSingleton(aggregateValidatorType, aggregateValidator); } var eventValidatorType = typeof(EventValidator); @@ -20,7 +20,7 @@ public static IServiceCollection AddValidationFrom(this IServiceCollection servi sourceAssembly.GetTypes().Where(t => t.IsAssignableTo(eventValidatorType)).ToList(); foreach (var eventValidator in eventValidators) { - services.AddScoped(eventValidatorType, eventValidator); + services.AddSingleton(eventValidatorType, eventValidator); } services.AddSingleton();