From 4a3e2e5bdbe17b9437b0875f27dfc71f6edaecc6 Mon Sep 17 00:00:00 2001 From: Trent Gardner Date: Mon, 23 Sep 2024 21:33:31 +0930 Subject: [PATCH] Fixed test --- .../DependencyInjectionCommandCreator.cs | 3 +-- src/Oakton/HostedCommandExtensions.cs | 12 ++++----- src/Tests/HostedCommandsTester.cs | 26 ++++++++++++------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Oakton/DependencyInjectionCommandCreator.cs b/src/Oakton/DependencyInjectionCommandCreator.cs index 14ba393b..27a8ff18 100644 --- a/src/Oakton/DependencyInjectionCommandCreator.cs +++ b/src/Oakton/DependencyInjectionCommandCreator.cs @@ -13,8 +13,7 @@ public DependencyInjectionCommandCreator(IServiceProvider serviceProvider) public IOaktonCommand CreateCommand(Type commandType) { - using var scope = _serviceProvider.CreateScope(); - return ActivatorUtilities.CreateInstance(scope.ServiceProvider, commandType) as IOaktonCommand; + return ActivatorUtilities.CreateInstance(_serviceProvider, commandType) as IOaktonCommand; } public object CreateModel(Type modelType) diff --git a/src/Oakton/HostedCommandExtensions.cs b/src/Oakton/HostedCommandExtensions.cs index 4fb0b0d9..8024dc96 100644 --- a/src/Oakton/HostedCommandExtensions.cs +++ b/src/Oakton/HostedCommandExtensions.cs @@ -23,9 +23,9 @@ public static void AddOakton(this IServiceCollection services, Action(); + services.TryAddScoped(); - services.TryAddSingleton((ctx) => + services.TryAddScoped((ctx) => { var creator = ctx.GetRequiredService(); var oaktonOptions = ctx.GetRequiredService>().Value; @@ -36,7 +36,7 @@ public static void AddOakton(this IServiceCollection services, Action(); + services.TryAddScoped(); } /// @@ -59,14 +59,14 @@ public static int RunHostedOaktonCommands(this IHost host, string[] args) /// /// An already built IHost /// - /// Optionally configure additional command options /// public static Task RunHostedOaktonCommandsAsync(this IHost host, string[] args) { - var options = host.Services.GetRequiredService>().Value; + using var scope = host.Services.CreateScope(); + var options = scope.ServiceProvider.GetRequiredService>().Value; args = ApplyArgumentDefaults(args, options); - var executor = host.Services.GetRequiredService(); + var executor = scope.ServiceProvider.GetRequiredService(); if (executor.Factory is CommandFactory factory) { diff --git a/src/Tests/HostedCommandsTester.cs b/src/Tests/HostedCommandsTester.cs index ed4cae5a..f48be1c5 100644 --- a/src/Tests/HostedCommandsTester.cs +++ b/src/Tests/HostedCommandsTester.cs @@ -14,7 +14,7 @@ public void CanInjectServicesIntoCommands() var builder = Host.CreateDefaultBuilder() .ConfigureServices(services => { - services.AddSingleton(); + services.AddScoped(); services.AddOakton(options => { options.Factory = factory => @@ -36,9 +36,23 @@ public class TestInput { } - public record TestDependency(int Value = 1); + public class TestDependency : IDisposable + { + public int Value { get; private set; } - public class TestDICommand : OaktonCommand, IDisposable + public TestDependency() + { + Value = 1; + } + + public void Dispose() + { + Value = 0; + GC.SuppressFinalize(this); + } + } + + public class TestDICommand : OaktonCommand { public static int Value { get; set; } = 0; private readonly TestDependency _dep; @@ -52,11 +66,5 @@ public override bool Execute(TestInput input) Value = _dep.Value; return true; } - - public void Dispose() - { - Value = 0; - GC.SuppressFinalize(this); - } } }