public async Task <ContentResult> Index()
        {
            var healthCheckResponse = await HealthCheck.GetRunner().RunAsync();

            Response.StatusCode = healthCheckResponse.StatusCode;

            return(Content(healthCheckResponse.Serialize(true), healthCheckResponse.ContentType));
        }
        private async Task RunHealthCheck()
        {
            var healthCheckResponse = await HealthCheck.GetRunner().RunAsync();

            Response.Clear();
            Response.StatusCode  = healthCheckResponse.StatusCode;
            Response.ContentType = healthCheckResponse.ContentType;
            Response.Write(healthCheckResponse.Serialize(true));
            Response.End();
        }
        /// <summary>
        /// Adds a terminal <see cref="HealthCheckMiddleware"/> to the application.
        /// </summary>
        /// <param name="builder">The application builder.</param>
        /// <param name="healthCheckRunner">
        /// The <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/>,
        /// the value of the <see cref="HealthCheck.GetRunner"/> method is used.
        /// </param>
        /// <param name="route">The route of the health endpoint.</param>
        /// <param name="indent">Whether to indent the JSON output.</param>
        /// <returns>The application builder.</returns>
        public static IApplicationBuilder UseRockLibHealthChecks(this IApplicationBuilder builder,
                                                                 IHealthCheckRunner healthCheckRunner, string route = "/health", bool indent = false)
        {
            healthCheckRunner = healthCheckRunner ?? HealthCheck.GetRunner();
            route             = $"/{route.Trim('/')}";

            return(builder
                   .Map(new PathString(route), appBuilder =>
            {
                appBuilder.UseMiddleware <HealthCheckMiddleware>(healthCheckRunner, indent);
            }));
        }
        /// <summary>
        /// Adds a health check route to the route collection.
        /// </summary>
        /// <param name="routes">The route collection.</param>
        /// <param name="healthCheckRunner">
        /// The <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/>,
        /// the value of the <see cref="HealthCheck.GetRunner"/> method is used.
        /// </param>
        /// <param name="route">The route of the health endpoint.</param>
        /// <param name="indent">Whether to indent the JSON output.</param>
        public static void MapHealthRoute(this HttpRouteCollection routes,
                                          IHealthCheckRunner healthCheckRunner, string route = "/health", bool indent = false)
        {
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }
            if (string.IsNullOrWhiteSpace(route))
            {
                throw new ArgumentNullException(nameof(route));
            }

            healthCheckRunner = healthCheckRunner ?? HealthCheck.GetRunner();
            route             = route.Trim('/');

            var routeIndex = Interlocked.Increment(ref _routeIndex);
            var routeName  = routeIndex < 1 ? "HealthApi" : $"HealthApi-{routeIndex}";

            routes.MapHttpRoute(routeName, route, null, null, new HealthCheckHandler(healthCheckRunner, indent));
        }
Example #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddSingleton(serviceProvider => HealthCheck.GetRunner());
        }
 /// <summary>
 /// Adds a health check route to the route collection.
 /// </summary>
 /// <param name="routes">The route collection.</param>
 /// <param name = "healthCheckRunnerName" >
 /// The name of the <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/> or not provided,
 /// the default value of the <see cref="HealthCheck.GetRunner"/> method is used.
 /// </param>
 /// <param name="route">The route of the health endpoint.</param>
 /// <param name="indent">Whether to indent the JSON output.</param>
 public static void MapHealthRoute(this HttpRouteCollection routes,
                                   string healthCheckRunnerName = null, string route = "/health", bool indent = false)
 => routes.MapHealthRoute(HealthCheck.GetRunner(healthCheckRunnerName), route, indent);
 /// <summary>
 /// Adds a terminal <see cref="HealthCheckMiddleware"/> to the application.
 /// </summary>
 /// <param name="builder">The application builder.</param>
 /// <param name="healthCheckRunnerName">
 /// The name of the <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/> or not provided,
 /// the default value of the <see cref="HealthCheck.GetRunner"/> method is used.
 /// </param>
 /// <param name="route">The route of the health endpoint.</param>
 /// <param name="indent">Whether to indent the JSON output.</param>
 /// <returns>The application builder.</returns>
 public static IApplicationBuilder UseRockLibHealthChecks(this IApplicationBuilder builder,
                                                          string healthCheckRunnerName = null, string route = "/health", bool indent = false)
 => builder.UseRockLibHealthChecks(HealthCheck.GetRunner(healthCheckRunnerName), route, indent);