/// <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(_checks, 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(HealthCheckStatus)} 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.ResponseWriter != null)
            {
                await _healthCheckOptions.ResponseWriter(httpContext, result);
            }
        }
Esempio n. 2
0
        /// <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));
            }
            if (!httpContext.User.Identity.IsAuthenticated)
            {
                httpContext.Response.StatusCode = 401;//= 401;
                //await httpContext.ChallengeAsync();
            }
            else
            {
                // 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[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);
                }
            }
        }