Skip to content

Commit 046dd0e

Browse files
committed
Simplify DriverService.IsInitialized
1 parent 8fc36d2 commit 046dd0e

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,27 @@ protected virtual bool IsInitialized
177177
{
178178
get
179179
{
180-
bool isInitialized = false;
181-
182180
try
183181
{
184-
using (var httpClient = new HttpClient())
185-
{
186-
httpClient.DefaultRequestHeaders.ConnectionClose = true;
187-
httpClient.Timeout = TimeSpan.FromSeconds(5);
182+
using var httpClient = new HttpClient();
183+
httpClient.DefaultRequestHeaders.ConnectionClose = true;
184+
httpClient.Timeout = TimeSpan.FromSeconds(5);
188185

189-
Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
190-
using (var response = Task.Run(async () => await httpClient.GetAsync(serviceHealthUri)).GetAwaiter().GetResult())
191-
{
192-
// Checking the response from the 'status' end point. Note that we are simply checking
193-
// that the HTTP status returned is a 200 status, and that the resposne has the correct
194-
// Content-Type header. A more sophisticated check would parse the JSON response and
195-
// validate its values. At the moment we do not do this more sophisticated check.
196-
isInitialized = response.StatusCode == HttpStatusCode.OK && response.Content.Headers.ContentType is { MediaType: string mediaType } && mediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
197-
}
198-
}
186+
Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
187+
using var response = Task.Run(async () => await httpClient.GetAsync(serviceHealthUri)).GetAwaiter().GetResult();
188+
189+
// Checking the response from the 'status' end point. Note that we are simply checking
190+
// that the HTTP status returned is a 200 status, and that the response has the correct
191+
// Content-Type header. A more sophisticated check would parse the JSON response and
192+
// validate its values. At the moment we do not do this more sophisticated check.
193+
bool isInitialized = response.StatusCode == HttpStatusCode.OK && response.Content.Headers.ContentType is { MediaType: string mediaType } && mediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
194+
195+
return isInitialized;
199196
}
200197
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
201198
{
202-
// Do nothing. The exception is expected, meaning driver service is not initialized.
199+
return false;
203200
}
204-
205-
return isInitialized;
206201
}
207202
}
208203

0 commit comments

Comments
 (0)