Skip to content

Commit 180b4ad

Browse files
authored
[SPA] Centralize HttpClient creation in SpaProxyLaunchManager (#33164)
* Create HttpClient instances in one place to ensure they are configured with the same settings. * Add a quality header to the `*/*` media type on the accept header.
1 parent 5fae4d6 commit 180b4ad

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ public SpaProxyLaunchManager(
3333

3434
public void StartInBackground(CancellationToken cancellationToken)
3535
{
36-
var httpClient = new HttpClient(new HttpClientHandler()
37-
{
38-
// It's ok for us to do this here since this service is only plugged in during development.
39-
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
40-
});
41-
// Certain frontend build tools rely on the the ACCEPT Header being set, otherwise these tools
42-
// might be unable to perform their client-side fallback logic.
43-
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
4436
_logger.LogInformation($"No SPA development server running at {_options.ServerUrl} found.");
4537

4638
// We are not waiting for the SPA proxy to launch, instead we are going to rely on a piece of
@@ -53,7 +45,7 @@ public void StartInBackground(CancellationToken cancellationToken)
5345
{
5446
if (_launchTask == null)
5547
{
56-
_launchTask = UpdateStatus(StartSpaProcessAndProbeForLiveness(httpClient, cancellationToken));
48+
_launchTask = UpdateStatus(StartSpaProcessAndProbeForLiveness(cancellationToken));
5749
}
5850
}
5951

@@ -79,11 +71,7 @@ async Task UpdateStatus(Task launchTask)
7971

8072
public async Task<bool> IsSpaProxyRunning(CancellationToken cancellationToken)
8173
{
82-
var httpClient = new HttpClient(new HttpClientHandler()
83-
{
84-
// It's ok for us to do this here since this service is only plugged in during development.
85-
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
86-
});
74+
var httpClient = CreateHttpClient();
8775

8876
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
8977
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken);
@@ -102,6 +90,18 @@ exception is TaskCanceledException ||
10290
}
10391
}
10492

93+
private static HttpClient CreateHttpClient()
94+
{
95+
var httpClient = new HttpClient(new HttpClientHandler()
96+
{
97+
// It's ok for us to do this here since this service is only plugged in during development.
98+
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
99+
});
100+
// We don't care about the returned content type as long as the server is able to answer with 2XX
101+
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*", 0.1));
102+
return httpClient;
103+
}
104+
105105
private async Task<bool> ProbeSpaDevelopmentServerUrl(HttpClient httpClient, CancellationToken cancellationToken)
106106
{
107107
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
@@ -121,12 +121,13 @@ exception is TaskCanceledException ||
121121
}
122122
}
123123

124-
private async Task StartSpaProcessAndProbeForLiveness(HttpClient httpClient, CancellationToken cancellationToken)
124+
private async Task StartSpaProcessAndProbeForLiveness(CancellationToken cancellationToken)
125125
{
126126
LaunchDevelopmentProxy();
127127
var sw = Stopwatch.StartNew();
128128
var livenessProbeSucceeded = false;
129129
var maxTimeoutReached = false;
130+
var httpClient = CreateHttpClient();
130131
while (_spaProcess != null && !_spaProcess.HasExited && !maxTimeoutReached)
131132
{
132133
livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl(httpClient, cancellationToken);

0 commit comments

Comments
 (0)