11using System . Collections . Generic ;
2- using System . CommandLine . Binding ;
32using System . CommandLine . Invocation ;
4- using System . CommandLine . NamingConventionBinder ;
53using System . Linq ;
64using System . Threading ;
75using System . Threading . Tasks ;
1210namespace System . CommandLine . Hosting
1311{
1412 // It's a wrapper, that configures the host, starts it and then runs the actual action.
15- internal sealed class HostingAction : BindingHandler
13+ internal sealed class HostingWrappingAction : AsynchronousCommandLineAction
1614 {
1715 internal const string HostingDirectiveName = "config" ;
1816
@@ -22,7 +20,7 @@ internal sealed class HostingAction : BindingHandler
2220
2321 internal static void SetHandlers ( Command command , Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost )
2422 {
25- command . Action = new HostingAction ( hostBuilderFactory , configureHost , ( AsynchronousCommandLineAction ) command . Action ) ;
23+ command . Action = new HostingWrappingAction ( hostBuilderFactory , configureHost , ( AsynchronousCommandLineAction ) command . Action ) ;
2624 command . TreatUnmatchedTokensAsErrors = false ; // to pass unmatched Tokens to host builder factory
2725
2826 foreach ( Command subCommand in command . Subcommands )
@@ -31,18 +29,13 @@ internal static void SetHandlers(Command command, Func<string[], IHostBuilder> h
3129 }
3230 }
3331
34- private HostingAction ( Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost , AsynchronousCommandLineAction actualAction )
32+ private HostingWrappingAction ( Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost , AsynchronousCommandLineAction actualAction )
3533 {
3634 _hostBuilderFactory = hostBuilderFactory ;
3735 _configureHost = configureHost ;
3836 _actualAction = actualAction ;
3937 }
4038
41- public override BindingContext GetBindingContext ( ParseResult parseResult )
42- => _actualAction is BindingHandler bindingHandler
43- ? bindingHandler . GetBindingContext ( parseResult )
44- : base . GetBindingContext ( parseResult ) ;
45-
4639 public override async Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken = default )
4740 {
4841 var argsRemaining = parseResult . UnmatchedTokens ;
@@ -76,35 +69,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
7669 }
7770 }
7871
79- var bindingContext = GetBindingContext ( parseResult ) ;
80- int registeredBefore = 0 ;
81- hostBuilder . ConfigureServices ( services =>
72+ hostBuilder . ConfigureServices ( static ( context , services ) =>
8273 {
74+ var parseResult = context . GetParseResult ( ) ;
8375 services . AddSingleton ( parseResult ) ;
84- services . AddSingleton ( bindingContext ) ;
85-
86- registeredBefore = services . Count ;
8776 } ) ;
8877
89- if ( _configureHost is not null )
90- {
91- _configureHost . Invoke ( hostBuilder ) ;
92-
93- hostBuilder . ConfigureServices ( services =>
94- {
95- // "_configureHost" just registered types that might be needed in BindingContext
96- for ( int i = registeredBefore ; i < services . Count ; i ++ )
97- {
98- Type captured = services [ i ] . ServiceType ;
99- bindingContext . AddService ( captured , c => c . GetService < IHost > ( ) . Services . GetService ( captured ) ) ;
100- }
101- } ) ;
102- }
78+ _configureHost ? . Invoke ( hostBuilder ) ;
10379
10480 using var host = hostBuilder . Build ( ) ;
10581
106- bindingContext . AddService ( typeof ( IHost ) , _ => host ) ;
107-
10882 await host . StartAsync ( cancellationToken ) ;
10983
11084 try
0 commit comments