public void Install(IServiceCollection services, IConfigurationRoot configuration) { // health checks // telemetry recorder string instrumentationKey = configuration["ApplicationInsights:InstrumentationKey"]; string endpointAddress = configuration["ApplicationInsights:EndpointAddress"]; if (!string.IsNullOrEmpty(instrumentationKey) && !string.IsNullOrEmpty(endpointAddress)) { TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(instrumentationKey, new InMemoryChannel { EndpointAddress = endpointAddress }); TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration); services.AddSingleton(telemetryClient); services.AddTransient <IAvailabilityRecorder, ApplicationInsightsRecorder>(); } else { services.AddTransient <IAvailabilityRecorder, NullRecorder>(); } // configuration services.AddSingleton(configuration.GetSection("HealthCheckHostedService").Get <HealthCheckServiceConfiguration>()); services.AddSingleton(configuration.GetSection("Build").Get <BuildModel>()); // checks services.AddTransient <UrlCheck>(); services.AddTransient <DbContextCheck>(); services.AddTransient <ExampleCheck>(); // check factory and hosted service services.AddSingleton(sp => { var cache = sp.GetService <IMemoryCache>(); var logger = sp.GetService <ILogger <Check> >(); var recorder = sp.GetService <IAvailabilityRecorder>(); var configuration = sp.GetService <IConfiguration>(); var factory = new CheckFactory(cache, logger, recorder, sp, configuration); factory.RegisterCheck("example", typeof(ExampleCheck)); return(factory as ICheckFactory); }); services.AddHostedService <HealthCheckHostedService>(); }
public void ShouldResolveUnresolvedCheckForUnregisteredType() { // assert var cache = new Mock <IMemoryCache>(); var logger = new Mock <ILogger <Check> >(); var recorder = new Mock <IAvailabilityRecorder>(); var configuration = new Mock <IConfiguration>(); var sp = serviceCollection.BuildServiceProvider(); var factory = new CheckFactory(cache.Object, logger.Object, recorder.Object, sp, configuration.Object); factory.RegisterCheck("foo", typeof(HealthCheck)); // act var config = new CheckConfiguration() { Name = "foo", Type = "foo" }; var check = factory.Create(config); // assert check.Should().NotBeNull(); check.Should().BeOfType(typeof(UnresolvedCheck)); }