diff --git a/src/Hosting.CommandLine/HostBuilderExtensions.cs b/src/Hosting.CommandLine/HostBuilderExtensions.cs index 6bcb4e9c..882057d5 100644 --- a/src/Hosting.CommandLine/HostBuilderExtensions.cs +++ b/src/Hosting.CommandLine/HostBuilderExtensions.cs @@ -35,7 +35,7 @@ public static async Task RunCommandLineApplicationAsync( CancellationToken cancellationToken = default) where TApp : class { - return await RunCommandLineApplicationAsync(hostBuilder, args, app => { }, cancellationToken); + return await RunCommandLineApplicationAsync(hostBuilder, args, null, cancellationToken); } /// @@ -56,18 +56,7 @@ public static async Task RunCommandLineApplicationAsync( CancellationToken cancellationToken = default) where TApp : class { - var state = new CommandLineState(args); - hostBuilder.Properties[typeof(CommandLineState)] = state; - hostBuilder.ConfigureServices( - (context, services) - => - { - services.AddCommonServices(state); - services.AddSingleton>(); - services.AddSingleton(configure); - }); - - using var host = hostBuilder.Build(); + using var host = hostBuilder.UseCommandLineApplication(args, configure).Build(); return await host.RunCommandLineApplicationAsync(cancellationToken); } @@ -88,16 +77,14 @@ public static async Task RunCommandLineApplicationAsync( Action configure, CancellationToken cancellationToken = default) { + configure ??= _ => { }; var state = new CommandLineState(args); hostBuilder.Properties[typeof(CommandLineState)] = state; - hostBuilder.ConfigureServices( - (context, services) - => - { - services.AddCommonServices(state); - services.AddSingleton(); - services.AddSingleton(configure); - }); + hostBuilder.ConfigureServices((context, services) => + services + .AddCommonServices(state) + .AddSingleton() + .AddSingleton(configure)); using var host = hostBuilder.Build(); return await host.RunCommandLineApplicationAsync(cancellationToken); @@ -131,50 +118,32 @@ public static IHostBuilder UseCommandLineApplication( Action> configure) where TApp : class { - configure ??= app => { }; + configure ??= _ => { }; var state = new CommandLineState(args); hostBuilder.Properties[typeof(CommandLineState)] = state; - hostBuilder.ConfigureServices( - (context, services) => - { - services - .TryAddSingleton(); - services - .TryAddSingleton(provider => provider.GetRequiredService()); - services - .AddSingleton() - .TryAddSingleton(PhysicalConsole.Singleton); - services - .AddSingleton(provider => - { - state.SetConsole(provider.GetService()); - return state; - }) - .AddSingleton(state) - .AddSingleton>(); - services - .AddSingleton(configure); - }); + hostBuilder.ConfigureServices((context, services) => + services + .AddCommonServices(state) + .AddSingleton>() + .AddSingleton(configure)); return hostBuilder; } - private static void AddCommonServices(this IServiceCollection services, CommandLineState state) + private static IServiceCollection AddCommonServices(this IServiceCollection services, CommandLineState state) { - services - .TryAddSingleton(); - services - .TryAddSingleton(provider => provider.GetRequiredService()); + services.TryAddSingleton(); + services.TryAddSingleton(provider => provider.GetRequiredService()); + services.TryAddSingleton(PhysicalConsole.Singleton); services .AddSingleton() - .TryAddSingleton(PhysicalConsole.Singleton); - services .AddSingleton(provider => { state.SetConsole(provider.GetService()); return state; }) .AddSingleton(state); + return services; } } } diff --git a/src/Hosting.CommandLine/HostExtensions.cs b/src/Hosting.CommandLine/HostExtensions.cs index 5e006f31..7606faaf 100644 --- a/src/Hosting.CommandLine/HostExtensions.cs +++ b/src/Hosting.CommandLine/HostExtensions.cs @@ -1,16 +1,16 @@ // Copyright (c) Nate McMaster. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using System.Runtime.ExceptionServices; +using System.Threading; +using System.Threading.Tasks; +using McMaster.Extensions.CommandLineUtils; +using McMaster.Extensions.Hosting.CommandLine.Internal; +using Microsoft.Extensions.DependencyInjection; + namespace Microsoft.Extensions.Hosting { - using System; - using System.Runtime.ExceptionServices; - using System.Threading; - using System.Threading.Tasks; - using McMaster.Extensions.CommandLineUtils; - using McMaster.Extensions.Hosting.CommandLine.Internal; - using Microsoft.Extensions.DependencyInjection; - /// /// Extension methods for support. ///