/// <summary> /// Execute the HealthCheck request. /// </summary> /// <param name="hc">HealthCheck description.</param> /// <param name="partition">Partition instance.</param> /// <returns>HealthCheck instance.</returns> internal async Task <HealthCheck> ExecuteHealthCheckAsync(HealthCheck hc, Partition partition) { // Check passed parameters. if ((null == partition) || (default(HealthCheck) == hc)) { return(default(HealthCheck)); } // Get the service endpoint of the service being tested. ResolvedServiceEndpoint rse = await this.GetServiceEndpointAsync(hc.ServiceName, partition); if (null == rse) { return(default(HealthCheck)); } // If an endpoint name was specified, search for that name within the ResolvedServiceEndpoint instance. string baseAddress = (string.IsNullOrWhiteSpace(hc.Endpoint)) ? rse.GetFirstEndpoint() : rse.GetEndpoint(hc.Endpoint); Uri uri = new Uri($"{baseAddress}/{hc.SuffixPath}"); // Create the HttpRequest message. HttpRequestMessage request = this.CreateRequestMessage(hc, uri); try { bool success = true; HealthState hs = HealthState.Ok; // Make the request to the service being tested. Stopwatch sw = Stopwatch.StartNew(); HttpResponseMessage response = await this._http.SendAsync(request, HttpCompletionOption.ResponseContentRead, this._token); sw.Stop(); // Evaluate the result of the request. If specific codes were provided, check each of the code arrays to find the result code. if ((null != hc.WarningStatusCodes) && (hc.WarningStatusCodes.Contains((int)response.StatusCode))) { hs = HealthState.Warning; success = false; } else if ((null != hc.ErrorStatusCodes) && (hc.ErrorStatusCodes.Contains((int)response.StatusCode))) { hs = HealthState.Error; success = false; } else if (false == response.StatusCode.IsSuccessCode()) { hs = HealthState.Error; success = false; } // Report health result to Service Fabric. this.Client.HealthManager.ReportHealth(new PartitionHealthReport(hc.Partition, new HealthInformation("Watchdog Health Check", hc.Name, hs))); // Report the availability of the tested service to the telemetry provider. await this._telemetry.ReportAvailabilityAsync( hc.ServiceName.AbsoluteUri, hc.Partition.ToString(), hc.Name, hc.LastAttempt, TimeSpan.FromMilliseconds(hc.Duration), null, success, this._token); // Return a new HealthCheck instance containing the results of the request. long count = (success) ? 0 : hc.FailureCount + 1; return(hc.UpdateWith(DateTime.UtcNow, count, sw.ElapsedMilliseconds, response.StatusCode)); } catch (FabricTransientException ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.ExecuteHealthCheckAsync)); return(hc.UpdateWith(DateTime.UtcNow, hc.FailureCount + 1, -1, System.Net.HttpStatusCode.InternalServerError)); } catch (Exception ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.ExecuteHealthCheckAsync)); throw; } }