public async Task Get() { var result = await _healthCheckService.CheckHealthAsync(_healthCheckOptions.Predicate, HttpContext.RequestAborted); // Map status to response code - this is customizable via options. if (!_healthCheckOptions.ResultStatusCodes.TryGetValue(result.Status, out var statusCode)) { var message = $"No status code mapping found for {nameof(HealthStatus)} value: {result.Status}." + $"{nameof(HealthCheckOptions)}.{nameof(HealthCheckOptions.ResultStatusCodes)} must contain" + $"an entry for {result.Status}."; throw new InvalidOperationException(message); } HttpContext.Response.StatusCode = statusCode; if (!_healthCheckOptions.AllowCachingResponses) { var headers = HttpContext.Response.Headers; headers[HeaderNames.CacheControl] = "no-store, no-cache"; headers[HeaderNames.Pragma] = "no-cache"; headers[HeaderNames.Expires] = "Thu, 01 Jan 1970 00:00:00 GMT"; } if (_healthCheckOptions.ResponseWriter != null) { await _healthCheckOptions.ResponseWriter(HttpContext, result); } }
/// <summary> /// Processes a request. /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public async Task InvokeAsync(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } // Get results var result = await _healthCheckService.CheckHealthAsync(_healthCheckOptions.Predicate, httpContext.RequestAborted); // Map status to response code - this is customizable via options. if (!_healthCheckOptions.ResultStatusCodes.TryGetValue(result.Status, out var statusCode)) { var message = $"No status code mapping found for {nameof(HealthStatus)} value: {result.Status}." + $"{nameof(HealthCheckOptions)}.{nameof(HealthCheckOptions.ResultStatusCodes)} must contain" + $"an entry for {result.Status}."; throw new InvalidOperationException(message); } httpContext.Response.StatusCode = statusCode; if (!_healthCheckOptions.AllowCachingResponses) { // Similar to: https://github.com/aspnet/Security/blob/7b6c9cf0eeb149f2142dedd55a17430e7831ea99/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationHandler.cs#L377-L379 var headers = httpContext.Response.Headers; headers.CacheControl = "no-store, no-cache"; headers.Pragma = "no-cache"; headers.Expires = "Thu, 01 Jan 1970 00:00:00 GMT"; } if (_healthCheckOptions.ResponseWriter != null) { await _healthCheckOptions.ResponseWriter(httpContext, result); } }