@@ -139,17 +139,35 @@ public async Task Run(CancellationToken? cancellationToken) {
139139 }
140140
141141 // wait here (You can use something else as a wait function, I am using this as a demo)
142- Console . WriteLine ( "Press Enter to stop the Microsoft Graph Developer Proxy" ) ;
143- Console . ReadLine ( ) ;
142+ Console . WriteLine ( "Press CTRL+C to stop the Microsoft Graph Developer Proxy" ) ;
143+ Console . CancelKeyPress += Console_CancelKeyPress ;
144+ // wait for the proxy to stop
145+ while ( _proxyServer . ProxyRunning ) { Thread . Sleep ( 10 ) ; }
146+ }
147+
148+ private void Console_CancelKeyPress ( object ? sender , ConsoleCancelEventArgs e ) {
149+ StopProxy ( ) ;
150+ }
144151
152+ private void StopProxy ( ) {
145153 // Unsubscribe & Quit
146- _explicitEndPoint . BeforeTunnelConnectRequest -= OnBeforeTunnelConnectRequest ;
147- _proxyServer . BeforeRequest -= OnRequest ;
148- _proxyServer . BeforeResponse -= OnResponse ;
149- _proxyServer . ServerCertificateValidationCallback -= OnCertificateValidation ;
150- _proxyServer . ClientCertificateSelectionCallback -= OnCertificateSelection ;
154+ try {
155+ if ( _explicitEndPoint != null ) {
156+ _explicitEndPoint . BeforeTunnelConnectRequest -= OnBeforeTunnelConnectRequest ;
157+ }
151158
152- _proxyServer . Stop ( ) ;
159+ if ( _proxyServer != null ) {
160+ _proxyServer . BeforeRequest -= OnRequest ;
161+ _proxyServer . BeforeResponse -= OnResponse ;
162+ _proxyServer . ServerCertificateValidationCallback -= OnCertificateValidation ;
163+ _proxyServer . ClientCertificateSelectionCallback -= OnCertificateSelection ;
164+
165+ _proxyServer . Stop ( ) ;
166+ }
167+ }
168+ catch ( Exception ex ) {
169+ Console . WriteLine ( $ "Exception: { ex . Message } ") ;
170+ }
153171 }
154172
155173 private void OnCancellation ( ) {
0 commit comments