1
1
using System . Collections . Generic ;
2
- using System . CommandLine . Binding ;
3
2
using System . CommandLine . Invocation ;
4
- using System . CommandLine . NamingConventionBinder ;
5
3
using System . Linq ;
6
4
using System . Threading ;
7
5
using System . Threading . Tasks ;
12
10
namespace System . CommandLine . Hosting
13
11
{
14
12
// 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
16
14
{
17
15
internal const string HostingDirectiveName = "config" ;
18
16
@@ -22,7 +20,7 @@ internal sealed class HostingAction : BindingHandler
22
20
23
21
internal static void SetHandlers ( Command command , Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost )
24
22
{
25
- command . Action = new HostingAction ( hostBuilderFactory , configureHost , ( AsynchronousCommandLineAction ) command . Action ) ;
23
+ command . Action = new HostingWrappingAction ( hostBuilderFactory , configureHost , ( AsynchronousCommandLineAction ) command . Action ) ;
26
24
command . TreatUnmatchedTokensAsErrors = false ; // to pass unmatched Tokens to host builder factory
27
25
28
26
foreach ( Command subCommand in command . Subcommands )
@@ -31,18 +29,13 @@ internal static void SetHandlers(Command command, Func<string[], IHostBuilder> h
31
29
}
32
30
}
33
31
34
- private HostingAction ( Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost , AsynchronousCommandLineAction actualAction )
32
+ private HostingWrappingAction ( Func < string [ ] , IHostBuilder > hostBuilderFactory , Action < IHostBuilder > configureHost , AsynchronousCommandLineAction actualAction )
35
33
{
36
34
_hostBuilderFactory = hostBuilderFactory ;
37
35
_configureHost = configureHost ;
38
36
_actualAction = actualAction ;
39
37
}
40
38
41
- public override BindingContext GetBindingContext ( ParseResult parseResult )
42
- => _actualAction is BindingHandler bindingHandler
43
- ? bindingHandler . GetBindingContext ( parseResult )
44
- : base . GetBindingContext ( parseResult ) ;
45
-
46
39
public override async Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken = default )
47
40
{
48
41
var argsRemaining = parseResult . UnmatchedTokens ;
@@ -76,35 +69,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
76
69
}
77
70
}
78
71
79
- var bindingContext = GetBindingContext ( parseResult ) ;
80
- int registeredBefore = 0 ;
81
- hostBuilder . ConfigureServices ( services =>
72
+ hostBuilder . ConfigureServices ( static ( context , services ) =>
82
73
{
74
+ var parseResult = context . GetParseResult ( ) ;
83
75
services . AddSingleton ( parseResult ) ;
84
- services . AddSingleton ( bindingContext ) ;
85
-
86
- registeredBefore = services . Count ;
87
76
} ) ;
88
77
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 ) ;
103
79
104
80
using var host = hostBuilder . Build ( ) ;
105
81
106
- bindingContext . AddService ( typeof ( IHost ) , _ => host ) ;
107
-
108
82
await host . StartAsync ( cancellationToken ) ;
109
83
110
84
try
0 commit comments