1
+ #if NET8_0_OR_GREATER
2
+ using System . Collections . Generic ;
3
+ using Microsoft . Extensions . Configuration ;
4
+ using Microsoft . Extensions . DependencyInjection ;
5
+ using Microsoft . Extensions . Hosting ;
6
+
7
+ namespace System . CommandLine . Hosting ;
8
+
9
+ public class HostApplicationBuilderAction ( ) : HostingAction ( )
10
+ {
11
+ private new readonly Func < string [ ] , HostApplicationBuilder > ? _createHostBuilder ;
12
+ public new Action < HostApplicationBuilder > ? ConfigureHost { get ; set ; }
13
+
14
+ protected override IHostBuilder CreateHostBuiderCore ( string [ ] args )
15
+ {
16
+ var hostBuilder = _createHostBuilder ? . Invoke ( args ) ??
17
+ new HostApplicationBuilder ( args ) ;
18
+ return new HostApplicationBuilderWrapper ( hostBuilder ) ;
19
+ }
20
+
21
+ protected override void ConfigureHostBuilder ( IHostBuilder hostBuilder )
22
+ {
23
+ base . ConfigureHostBuilder ( hostBuilder ) ;
24
+ ConfigureHost ? . Invoke ( GetHostApplicationBuilder ( hostBuilder ) ) ;
25
+ }
26
+
27
+ private static HostApplicationBuilder GetHostApplicationBuilder (
28
+ IHostBuilder hostBuilder
29
+ )
30
+ {
31
+ return ( HostApplicationBuilder ) hostBuilder
32
+ . Properties [ typeof ( HostApplicationBuilder ) ] ;
33
+ }
34
+
35
+ private class HostApplicationBuilderWrapper (
36
+ HostApplicationBuilder hostApplicationBuilder
37
+ ) : IHostBuilder
38
+ {
39
+ private Action ? _useServiceProviderFactoryAction ;
40
+ private object ? _configureServiceProviderBuilderAction ;
41
+
42
+ public HostBuilderContext Context { get ; } = new (
43
+ ( ( IHostApplicationBuilder ) hostApplicationBuilder ) . Properties
44
+ )
45
+ {
46
+ Configuration = hostApplicationBuilder . Configuration ,
47
+ HostingEnvironment = hostApplicationBuilder . Environment ,
48
+ Properties =
49
+ { { typeof ( HostApplicationBuilder ) , hostApplicationBuilder } }
50
+ } ;
51
+
52
+ public IDictionary < object , object > Properties =>
53
+ ( ( IHostApplicationBuilder ) hostApplicationBuilder ) . Properties ;
54
+
55
+ public IHost Build ( )
56
+ {
57
+ _useServiceProviderFactoryAction ? . Invoke ( ) ;
58
+ return hostApplicationBuilder . Build ( ) ;
59
+ }
60
+
61
+ public IHostBuilder ConfigureHostConfiguration (
62
+ Action < IConfigurationBuilder > configureDelegate
63
+ )
64
+ {
65
+ configureDelegate ? . Invoke ( hostApplicationBuilder . Configuration ) ;
66
+ return this ;
67
+ }
68
+
69
+ public IHostBuilder ConfigureAppConfiguration (
70
+ Action < HostBuilderContext , IConfigurationBuilder > configureDelegate
71
+ )
72
+ {
73
+ SynchronizeContext ( ) ;
74
+ configureDelegate ? . Invoke (
75
+ Context ,
76
+ hostApplicationBuilder . Configuration
77
+ ) ;
78
+ SynchronizeContext ( ) ;
79
+ return this ;
80
+ }
81
+
82
+ public IHostBuilder ConfigureServices (
83
+ Action < HostBuilderContext , IServiceCollection > configureDelegate
84
+ )
85
+ {
86
+ SynchronizeContext ( ) ;
87
+ configureDelegate ? . Invoke ( Context , hostApplicationBuilder . Services ) ;
88
+ SynchronizeContext ( ) ;
89
+ return this ;
90
+ }
91
+
92
+ IHostBuilder IHostBuilder . UseServiceProviderFactory < TContainerBuilder > (
93
+ IServiceProviderFactory < TContainerBuilder > factory
94
+ )
95
+ {
96
+ _useServiceProviderFactoryAction = ( ) =>
97
+ {
98
+ Action < TContainerBuilder > ? configureDelegate = null ;
99
+ if ( _configureServiceProviderBuilderAction is Action < HostBuilderContext , TContainerBuilder > configureDelegateWithContext )
100
+ {
101
+ configureDelegate = builder =>
102
+ {
103
+ SynchronizeContext ( ) ;
104
+ configureDelegateWithContext ( Context , builder ) ;
105
+ SynchronizeContext ( ) ;
106
+ } ;
107
+ }
108
+ hostApplicationBuilder . ConfigureContainer ( factory , configureDelegate ) ;
109
+ } ;
110
+ return this ;
111
+ }
112
+
113
+ IHostBuilder IHostBuilder . UseServiceProviderFactory < TContainerBuilder > (
114
+ Func < HostBuilderContext , IServiceProviderFactory < TContainerBuilder > > factory
115
+ )
116
+ {
117
+ _useServiceProviderFactoryAction = ( ) =>
118
+ {
119
+ Action < TContainerBuilder > ? configureDelegate = null ;
120
+ if ( _configureServiceProviderBuilderAction is Action < HostBuilderContext , TContainerBuilder > configureDelegateWithContext )
121
+ {
122
+ configureDelegate = builder =>
123
+ {
124
+ SynchronizeContext ( ) ;
125
+ configureDelegateWithContext ( Context , builder ) ;
126
+ SynchronizeContext ( ) ;
127
+ } ;
128
+ }
129
+ var factoryInstance = factory ( Context ) ;
130
+ hostApplicationBuilder . ConfigureContainer ( factoryInstance , configureDelegate ) ;
131
+ } ;
132
+ return this ;
133
+ }
134
+
135
+ IHostBuilder IHostBuilder . ConfigureContainer < TContainerBuilder > (
136
+ Action < HostBuilderContext , TContainerBuilder > configureDelegate
137
+ )
138
+ {
139
+ _configureServiceProviderBuilderAction = configureDelegate ;
140
+ return this ;
141
+ }
142
+
143
+ private void SynchronizeContext ( )
144
+ {
145
+ Context . Configuration = hostApplicationBuilder . Configuration ;
146
+ Context . HostingEnvironment = hostApplicationBuilder . Environment ;
147
+ }
148
+ }
149
+ }
150
+ #endif
0 commit comments