Example #1
0
        /// <summary>
        ///     Add Auto health check to the asp.net core application without configurations
        /// </summary>
        /// <param name="services"></param>
        /// <param name="setupAction">configurations</param>
        /// <returns></returns>
        public static IAutoHealthCheckBuilder AddAutoHealthCheck(
            this IServiceCollection services,
            Action <AutoHealthCheckConfigurations> setupAction = null)
        {
            services.AddSingleton <IRouteDiscover, RouteDiscover>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IHealthChecker, HealthChecker>();
            services.AddSingleton <IEndpointBuilder, EndpointBuilder>();
            services.AddSingleton <IInternalRouteInformationEvaluator, InternalRouteInformationEvaluator>();
            services.AddSingleton <IRouteEvaluator, DefaultRouteEvaluator>();
            services.AddSingleton <IEndpointMessageTranslator, EndpointMessageTranslator>();
            services.AddSingleton <IEndpointCaller, EndpointCaller>();
            services.AddSingleton <IProbesProcessor, ProbesProcessor>();
            services.AddHttpClient();

            // resolve options
            var options = new AutoHealthCheckConfigurations();

            setupAction?.Invoke(options);
            _contextAccessor = new AutoHealthCheckContextAccessor();
            _contextAccessor.SetConfigurations(options);
            services.AddSingleton <IAutoHealthCheckContextAccessor>(_contextAccessor);

            // check if the service need to run automatically
            if (options.AutomaticRunConfigurations.AutomaticRunEnabled)
            {
                services.AddSingleton <IHostedService, AutoHealthCheckProcess>();
            }

            return(new DefaultAutoHealthCheckBuilder(services));
        }
Example #2
0
        public void AutoHealthCheckContextAccessor_should_set_configurations()
        {
            // arrange
            var context = new AutoHealthCheckContextAccessor();

            // act
            context.SetConfigurations(new AutoHealthCheckConfigurations());
        }
Example #3
0
        public void AutoHealthCheckContextAccessor_should_fail_setting_null_configurations()
        {
            // arrange
            var context = new AutoHealthCheckContextAccessor();

            // act
            // assert
            Assert.Throws <ArgumentNullException>(() => context.SetConfigurations(null)); // should fail with null configs.
        }