@@ -18,11 +18,6 @@ internal class SpaProxyLaunchManager : IHostedService, IDisposable
18
18
{
19
19
private readonly SpaDevelopmentServerOptions _options ;
20
20
private readonly ILogger < SpaProxyLaunchManager > _logger ;
21
- private readonly HttpClient _httpClient = new ( new HttpClientHandler ( )
22
- {
23
- // It's ok for us to do this here since this service is only plugged in during development.
24
- ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator
25
- } ) ;
26
21
27
22
private Process ? _spaProcess ;
28
23
private bool _disposedValue ;
@@ -39,50 +34,63 @@ public SpaProxyLaunchManager(ILogger<SpaProxyLaunchManager> logger)
39
34
40
35
public async Task StartAsync ( CancellationToken cancellationToken )
41
36
{
37
+ var httpClient = new HttpClient ( new HttpClientHandler ( )
38
+ {
39
+ // It's ok for us to do this here since this service is only plugged in during development.
40
+ ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator
41
+ } ) ;
42
+
42
43
_logger . LogInformation ( "Starting SPA development server" ) ;
43
- var running = await ProbeSpaDevelopmentServerUrl ( cancellationToken ) ;
44
+ var running = await ProbeSpaDevelopmentServerUrl ( httpClient , cancellationToken ) ;
44
45
if ( running )
45
46
{
46
47
_logger . LogInformation ( $ "Found SPA development server running at { _options . ServerUrl } ") ;
47
48
}
48
49
else
49
50
{
50
51
_logger . LogInformation ( $ "No SPA development server running at { _options . ServerUrl } found.") ;
51
- await StartSpaProcessAndProbeForLiveness ( cancellationToken ) ;
52
+ await StartSpaProcessAndProbeForLiveness ( httpClient , cancellationToken ) ;
52
53
}
53
54
}
54
55
55
- private async Task < bool > ProbeSpaDevelopmentServerUrl ( CancellationToken cancellationToken )
56
+ private async Task < bool > ProbeSpaDevelopmentServerUrl ( HttpClient httpClient , CancellationToken cancellationToken )
56
57
{
58
+ using var timeout = new CancellationTokenSource ( 1000 ) ;
59
+ using var cancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( timeout . Token , cancellationToken ) ;
57
60
try
58
61
{
59
- var response = await _httpClient . GetAsync ( _options . ServerUrl , cancellationToken ) ;
62
+ var response = await httpClient . GetAsync ( _options . ServerUrl , cancellationTokenSource . Token ) ;
60
63
var running = response . IsSuccessStatusCode ;
61
64
return running ;
62
65
}
63
- catch ( HttpRequestException httpException )
66
+ catch ( Exception exception ) when ( exception is HttpRequestException || exception is TaskCanceledException )
64
67
{
65
- _logger . LogDebug ( httpException , "Failed to connect to the SPA Development proxy." ) ;
68
+ _logger . LogDebug ( exception , "Failed to connect to the SPA Development proxy." ) ;
66
69
return false ;
67
70
}
68
71
}
69
72
70
- private async Task StartSpaProcessAndProbeForLiveness ( CancellationToken cancellationToken )
73
+ private async Task StartSpaProcessAndProbeForLiveness ( HttpClient httpClient , CancellationToken cancellationToken )
71
74
{
72
75
LaunchDevelopmentProxy ( ) ;
73
76
var sw = Stopwatch . StartNew ( ) ;
74
77
var livenessProbeSucceeded = false ;
75
78
var maxTimeoutReached = false ;
76
- while ( _spaProcess != null && ! _spaProcess . HasExited && ! livenessProbeSucceeded && ! maxTimeoutReached )
79
+ while ( _spaProcess != null && ! _spaProcess . HasExited && ! maxTimeoutReached )
77
80
{
78
- livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl ( cancellationToken ) ;
81
+ livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl ( httpClient , cancellationToken ) ;
79
82
if ( livenessProbeSucceeded )
80
83
{
81
84
break ;
82
85
}
83
86
87
+ if ( cancellationToken . IsCancellationRequested )
88
+ {
89
+ return ;
90
+ }
91
+
84
92
maxTimeoutReached = sw . Elapsed >= _options . MaxTimeout ;
85
- await Task . Delay ( 1000 ) ;
93
+ await Task . Delay ( 1000 , cancellationToken ) ;
86
94
}
87
95
88
96
if ( _spaProcess == null || _spaProcess . HasExited )
0 commit comments