in src/Diagnostics.HealthChecks/HealthChecksBuilderExtensions.cs [35:87]
public static IHealthChecksBuilder AddHttpEndpointCheck(
this IHealthChecksBuilder builder,
string name,
string endpointName,
string relativePath,
HttpMethod? method = null,
string? scheme = null,
IReadOnlyDictionary<string, IEnumerable<string>>? headers = null,
HttpStatusCode? expectedStatus = null,
HealthStatus failureStatus = HealthStatus.Unhealthy,
Func<HttpResponseMessage, HealthCheckResult, Task<HealthCheckResult>>? additionalCheck = null,
params KeyValuePair<string, object>[] reportData)
{
scheme = scheme == null
? Uri.UriSchemeHttp
: Uri.CheckSchemeName(scheme)
? scheme
: throw new ArgumentException("Invalid uri scheme", nameof(scheme));
if (!Uri.TryCreate(relativePath, UriKind.Relative, out Uri? result) || result.IsAbsoluteUri)
{
throw new ArgumentException("relativePath is not valid or can't be an Absolute uri", nameof(relativePath));
}
Func<UriBuilder, HttpRequestMessage> httpRequest = uriBuilder =>
{
uriBuilder.Path = relativePath;
uriBuilder.Scheme = scheme;
HttpRequestMessage requestMessage = new(method ?? HttpMethod.Get, uriBuilder.Uri);
if (headers != null)
{
foreach (KeyValuePair<string, IEnumerable<string>> pair in headers)
{
if (!requestMessage.Headers.TryAddWithoutValidation(pair.Key, pair.Value))
{
string errorMessage = string.Format(
CultureInfo.InvariantCulture,
"Cannot add request header with name '{0}' value '{1}' for health check '{2}'.",
pair.Key,
pair.Value,
name);
throw new ArgumentException(errorMessage, nameof(headers));
}
}
}
return requestMessage;
};
return builder.AddHttpEndpointCheck(name, endpointName, httpRequest, expectedStatus, failureStatus, additionalCheck, reportData);
}