@@ -43,15 +43,16 @@ public static async Task WaitForShutdownAsync(this IWebHost host, CancellationTo
43
43
var done = new ManualResetEventSlim ( false ) ;
44
44
using ( var cts = CancellationTokenSource . CreateLinkedTokenSource ( token ) )
45
45
{
46
- AttachCtrlcSigtermShutdown ( cts , done , shutdownMessage : string . Empty ) ;
47
-
48
- try
49
- {
50
- await host . WaitForTokenShutdownAsync ( cts . Token ) ;
51
- }
52
- finally
46
+ using ( var lifetime = new WebHostLifetime ( cts , done , shutdownMessage : string . Empty ) )
53
47
{
54
- done . Set ( ) ;
48
+ try
49
+ {
50
+ await host . WaitForTokenShutdownAsync ( cts . Token ) ;
51
+ }
52
+ finally
53
+ {
54
+ done . Set ( ) ;
55
+ }
55
56
}
56
57
}
57
58
}
@@ -84,15 +85,16 @@ public static async Task RunAsync(this IWebHost host, CancellationToken token =
84
85
using ( var cts = new CancellationTokenSource ( ) )
85
86
{
86
87
var shutdownMessage = host . Services . GetRequiredService < WebHostOptions > ( ) . SuppressStatusMessages ? string . Empty : "Application is shutting down..." ;
87
- AttachCtrlcSigtermShutdown ( cts , done , shutdownMessage : shutdownMessage ) ;
88
-
89
- try
90
- {
91
- await host . RunAsync ( cts . Token , "Application started. Press Ctrl+C to shut down." ) ;
92
- }
93
- finally
88
+ using ( var lifetime = new WebHostLifetime ( cts , done , shutdownMessage : shutdownMessage ) )
94
89
{
95
- done . Set ( ) ;
90
+ try
91
+ {
92
+ await host . RunAsync ( cts . Token , "Application started. Press Ctrl+C to shut down." ) ;
93
+ }
94
+ finally
95
+ {
96
+ done . Set ( ) ;
97
+ }
96
98
}
97
99
}
98
100
}
@@ -131,36 +133,6 @@ private static async Task RunAsync(this IWebHost host, CancellationToken token,
131
133
}
132
134
}
133
135
134
- private static void AttachCtrlcSigtermShutdown ( CancellationTokenSource cts , ManualResetEventSlim resetEvent , string shutdownMessage )
135
- {
136
- void Shutdown ( )
137
- {
138
- if ( ! cts . IsCancellationRequested )
139
- {
140
- if ( ! string . IsNullOrEmpty ( shutdownMessage ) )
141
- {
142
- Console . WriteLine ( shutdownMessage ) ;
143
- }
144
- try
145
- {
146
- cts . Cancel ( ) ;
147
- }
148
- catch ( ObjectDisposedException ) { }
149
- }
150
-
151
- // Wait on the given reset event
152
- resetEvent . Wait ( ) ;
153
- } ;
154
-
155
- AppDomain . CurrentDomain . ProcessExit += ( sender , eventArgs ) => Shutdown ( ) ;
156
- Console . CancelKeyPress += ( sender , eventArgs ) =>
157
- {
158
- Shutdown ( ) ;
159
- // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
160
- eventArgs . Cancel = true ;
161
- } ;
162
- }
163
-
164
136
private static async Task WaitForTokenShutdownAsync ( this IWebHost host , CancellationToken token )
165
137
{
166
138
var applicationLifetime = host . Services . GetService < IApplicationLifetime > ( ) ;
@@ -184,4 +156,4 @@ private static async Task WaitForTokenShutdownAsync(this IWebHost host, Cancella
184
156
await host . StopAsync ( ) ;
185
157
}
186
158
}
187
- }
159
+ }
0 commit comments