public override async Task <StatusCheckDetail> ExecuteStatusCheckAsync() { var timer = Stopwatch.StartNew(); try { // Generate a new HTTP request message var request = new HttpRequestMessage(_httpMethod, _uri); // Retrieve the response from the web service HttpResponseMessage response = await _httpClientFactory.CreateClient().SendAsync(request, new CancellationTokenSource(5000).Token).ConfigureAwait(false); // Evaluate that the response is good return(await EvaluateResponse(response).ConfigureAwait(false) ? new StatusCheckDetail(StatusTypes.OK, timer.ElapsedMilliseconds) : new StatusCheckDetail("Content check failed", timer.ElapsedMilliseconds)); } catch (Exception e) { _logger?.LogWarning(e, $"Unable to check web content at {_uri}. Reason: {e.Message}"); // Run TCP check to see if the service is responsive StatusCheckDetail tcpCheck = await TcpConnectionCheckAsync(_uri.GetComponents(UriComponents.Host, UriFormat.Unescaped), int.Parse(_uri.GetComponents(UriComponents.StrongPort, UriFormat.Unescaped)), timer).ConfigureAwait(false); return(tcpCheck.Value != StatusTypes.OK ? tcpCheck : new StatusCheckDetail(e.Message, timer.ElapsedMilliseconds)); } }
public static async Task <HealthCheckResult> CheckHealthAsync(ServiceStatusCheck check, HealthCheckContext context, CancellationToken cancellationToken = default) { try { StatusCheckDetail result = await check.ExecuteStatusCheckAsync().ConfigureAwait(false); return(new HealthCheckResult(result.Value == "OK" ? HealthStatus.Healthy : HealthStatus.Unhealthy, result.Value)); } catch (Exception ex) { return(new HealthCheckResult(HealthStatus.Unhealthy, exception: ex)); } }
/// <summary> /// /// </summary> /// <returns></returns> public override async Task <StatusCheckDetail> ExecuteStatusCheckAsync() { // Start a new timer var timer = Stopwatch.StartNew(); try { var connectionStringBuilder = new SqlConnectionStringBuilder(ResolveConnectionString()); try { // Create a connection to the server using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString)) { // Create a command to run on the server using (var command = new SqlCommand("SELECT 1 as ping", connection)) { // Set the command timeout command.CommandTimeout = 5; // Open the conection to the server await connection.OpenAsync().ConfigureAwait(false); // Run the command if ((int)await command.ExecuteScalarAsync().ConfigureAwait(false) == 1) { // Close the connection to the server connection.Close(); return(new StatusCheckDetail(StatusTypes.OK, timer.ElapsedMilliseconds)); } } throw new InvalidOperationException("We should never get here!"); } } catch (Exception e) { _logger?.LogWarning(e, $"Unable to check SQL server @ {connectionStringBuilder?.DataSource}.{connectionStringBuilder?.InitialCatalog}. Reason: {e.Message}"); // Default port number for SQL server is 1433 int port = 1433; // Check whether the data source has a port associated with it if (connectionStringBuilder.DataSource.Contains(",")) { port = int.Parse(connectionStringBuilder?.DataSource?.Split(',')?[1]); } // Run TCP check to see if the server is responsive StatusCheckDetail tcpCheck = await TcpConnectionCheckAsync(connectionStringBuilder?.DataSource, port, timer).ConfigureAwait(false); return(tcpCheck.Value != StatusTypes.OK ? tcpCheck : new StatusCheckDetail(e.Message, timer.ElapsedMilliseconds)); } } catch (Exception e) { _logger?.LogWarning(e, "Unable to parse connection string"); return(new StatusCheckDetail("Failed to parse connection string", timer.ElapsedMilliseconds)); } }