public static IServiceCollection AddEfCore <TDbContext>(this IServiceCollection services,
                                                                IHealthChecksBuilder healthChecksBuilder,
                                                                IConfiguration configuration,
                                                                Assembly fromAssembly) where TDbContext : DbContext
        {
            services
            .AddDbContext <DbContext, TDbContext>((provider, opts) =>
            {
                var connectionString = configuration.GetConnectionString("default");

                if (configuration.IsTyeEnabled())
                {
                    var databaseName = configuration.GetValue <string>("DatabaseName");
                    connectionString = configuration.GetConnectionString("sqlserver");
                    connectionString =
                        $"{connectionString};Initial Catalog={databaseName}";
                }

                opts.UseSqlServer(connectionString, context =>
                {
                    context.MigrationsAssembly(fromAssembly.GetName().Name);
                    context.EnableRetryOnFailure(maxRetryCount: 3);
                });
            })
            .AddHostedService <EfCoreMigrationHostedService>()
            .AddScoped(typeof(IPipelineBehavior <,>), typeof(PersistenceBehavior <,>));

            healthChecksBuilder.AddDbContextCheck <DbContext>();

            return(services);
        }
Пример #2
0
        /// <summary>
        ///     Add Health Checks dependencies varying on configuration.
        /// </summary>
        public static IServiceCollection AddHealthChecks(
            this IServiceCollection services,
            IConfiguration configuration)
        {
            if (services is null)
            {
                throw new System.ArgumentNullException(nameof(services));
            }

            if (configuration is null)
            {
                throw new System.ArgumentNullException(nameof(configuration));
            }

            IHealthChecksBuilder healthChecks = services.AddHealthChecks();

            IFeatureManager featureManager = services
                                             .BuildServiceProvider()
                                             .GetRequiredService <IFeatureManager>();

            bool isEnabled = featureManager
                             .IsEnabledAsync(nameof(CustomFeature.SQLServer))
                             .ConfigureAwait(false)
                             .GetAwaiter()
                             .GetResult();

            if (isEnabled)
            {
                healthChecks.AddDbContextCheck <DataContext>("DataContext");
            }

            return(services);
        }
        public static IHealthChecksBuilder AddSelfCheck(this IHealthChecksBuilder builder, string name, HealthStatus?failureStatus = null, IEnumerable <string> tags = null)
        {
            builder.AddCheck <SelfHealthCheck>(name, failureStatus ?? HealthStatus.Degraded, tags);
            builder.AddDbContextCheck <CompraContext>();

            return(builder);
        }
Пример #4
0
        public static IHealthChecksBuilder AddTorrentGreaseDataCheck(this IHealthChecksBuilder builder)
        {
            builder.AddDbContextCheck <TorrentGreaseDbContext>(customTestQuery: async(ctx, ct) =>
            {
                await ctx.Policies.CountAsync(ct).ConfigureAwait(false);
                return(true);
            });

            return(builder);
        }
        public static IHealthChecksBuilder AddGspDbHealthCheck <TContext>(
            this IHealthChecksBuilder builder,
            IConfiguration configuration,
            string migrationAssemblyName)
            where TContext : GspDbContext
        {
            var entityFrameworkConfiguration = configuration
                                               .GetSection(nameof(EntityFrameworkConfiguration))
                                               .Get <EntityFrameworkConfiguration>();

            return(builder.AddDbContextCheck <TContext>()
                   .AddMigrationSqlServerCheck <TContext>(entityFrameworkConfiguration, migrationAssemblyName));
        }
Пример #6
0
        public static IHealthChecksBuilder AddDatabaseContextCheck <TContext>(
            this IHealthChecksBuilder builder, IConfigurationSection configurationSection)
            where TContext : DbContext
        {
            var configuration = BindTo <BaseHealthCheckConfiguration>(configurationSection);

            return
                (builder
                 .AddDbContextCheck <TContext>(
                     configuration.Name,
                     configuration.HealthStatus,
                     configuration.Tags));
        }
        /// <summary>
        ///     Add Health Checks dependencies varying on configuration.
        /// </summary>
        public static IServiceCollection AddHealthChecks(
            this IServiceCollection services,
            IConfiguration configuration)
        {
            IHealthChecksBuilder healthChecks = services.AddHealthChecks();

            bool useFake = configuration.GetValue <bool>("PersistenceModule:UseFake");

            if (!useFake)
            {
                healthChecks.AddDbContextCheck <MangaContext>("MangaDbContext");
            }

            return(services);
        }
Пример #8
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSerilogWithInsights <Startup>(Configuration);

            services.AddInfrastructure(Configuration);

            IHealthChecksBuilder healthCheckBuilder = services.AddHealthChecks()
                                                      .AddDbContextCheck <ApplicationDbContext>();

            IIdentityServerBuilder builder = services.AddIdentityServer()
                                             .AddAspNetIdentity <ApplicationUser>()
                                             .AddProfileService <RolesProfileService>()
                                             .AddResourceOwnerValidator <ResourceOwnerPasswordValidator <ApplicationUser> >();

            if (Environment.IsDevelopment())
            {
                builder.AddDeveloperSigningCredential();

                builder.AddInMemoryIdentityResources(Config.Ids)
                .AddInMemoryApiResources(Config.Apis)
                .AddInMemoryClients(Config.Clients);
            }
            else
            {
                healthCheckBuilder
                .AddDbContextCheck <ISConfigurationDbContext>()
                .AddDbContextCheck <ISPersistedGrantDbContext>();

                builder.AddSigningCredential(GetCertificateAsync(Configuration).Result);

                var identityServerConnectionString = Configuration.GetConnectionString("IdentityServerConnectionString");

                builder
                .AddConfigurationStore <ISConfigurationDbContext>(options =>
                {
                    options.ConfigureDbContext = configurationDbBuilder =>
                                                 configurationDbBuilder.UseNpgsql(identityServerConnectionString);
                })
                .AddOperationalStore <ISPersistedGrantDbContext>(options =>
                {
                    options.ConfigureDbContext = operationalDbBuilder =>
                                                 operationalDbBuilder.UseNpgsql(identityServerConnectionString);

                    options.EnableTokenCleanup   = true;
                    options.TokenCleanupInterval = (int)TimeSpan.FromDays(1).TotalSeconds;
                });
            }
        }
Пример #9
0
        /// <summary>
        ///     Add Health Checks dependencies varying on configuration.
        /// </summary>
        public static IServiceCollection AddHealthChecks(
            this IServiceCollection services,
            IConfiguration configuration)
        {
            IHealthChecksBuilder healthChecks = services.AddHealthChecks();

            IFeatureManager featureManager = services
                                             .BuildServiceProvider()
                                             .GetRequiredService <IFeatureManager>();

            var isEnabled = featureManager
                            .IsEnabledAsync(nameof(CustomFeature.SqlServer))
                            .ConfigureAwait(false)
                            .GetAwaiter()
                            .GetResult();

            if (isEnabled)
            {
                healthChecks.AddDbContextCheck <KpmgContext>("KpmgContext");
            }

            return(services);
        }
        public static void AddIdSHealthChecks <TConfigurationDbContext, TPersistedGrantDbContext, TIdentityDbContext,
                                               TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>
            (this IHealthChecksBuilder healthChecksBuilder, AdminConfiguration adminConfiguration,
            ConnectionStringsConfiguration connectionStringsConfiguration, DatabaseProviderConfiguration databaseProviderConfiguration)
            where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext
            where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext
            where TIdentityDbContext : DbContext
            where TLogDbContext : DbContext, IAdminLogDbContext
            where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext <TAuditLog>
            where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext
            where TAuditLog : AuditLog
        {
            var configurationDbConnectionString   = connectionStringsConfiguration.ConfigurationDbConnection;
            var persistedGrantsDbConnectionString = connectionStringsConfiguration.PersistedGrantDbConnection;
            var identityDbConnectionString        = connectionStringsConfiguration.IdentityDbConnection;
            var logDbConnectionString             = connectionStringsConfiguration.AdminLogDbConnection;
            var auditLogDbConnectionString        = connectionStringsConfiguration.AdminAuditLogDbConnection;
            var dataProtectionDbConnectionString  = connectionStringsConfiguration.DataProtectionDbConnection;

            var identityServerUri = adminConfiguration.IdentityServerBaseUrl;

            healthChecksBuilder = healthChecksBuilder
                                  .AddDbContextCheck <TConfigurationDbContext>("ConfigurationDbContext")
                                  .AddDbContextCheck <TPersistedGrantDbContext>("PersistedGrantsDbContext")
                                  .AddDbContextCheck <TIdentityDbContext>("IdentityDbContext")
                                  .AddDbContextCheck <TLogDbContext>("LogDbContext")
                                  .AddDbContextCheck <TAuditLoggingDbContext>("AuditLogDbContext")
                                  .AddDbContextCheck <TDataProtectionDbContext>("DataProtectionDbContext")

                                  .AddIdentityServer(new Uri(identityServerUri), "Identity Server");

            var serviceProvider = healthChecksBuilder.Services.BuildServiceProvider();
            var scopeFactory    = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            using (var scope = scopeFactory.CreateScope())
            {
                var configurationTableName  = DbContextHelpers.GetEntityTable <TConfigurationDbContext>(scope.ServiceProvider);
                var persistedGrantTableName = DbContextHelpers.GetEntityTable <TPersistedGrantDbContext>(scope.ServiceProvider);
                var identityTableName       = DbContextHelpers.GetEntityTable <TIdentityDbContext>(scope.ServiceProvider);
                var logTableName            = DbContextHelpers.GetEntityTable <TLogDbContext>(scope.ServiceProvider);
                var auditLogTableName       = DbContextHelpers.GetEntityTable <TAuditLoggingDbContext>(scope.ServiceProvider);
                var dataProtectionTableName = DbContextHelpers.GetEntityTable <TDataProtectionDbContext>(scope.ServiceProvider);

                switch (databaseProviderConfiguration.ProviderType)
                {
                case DatabaseProviderType.SqlServer:
                    healthChecksBuilder
                    .AddSqlServer(configurationDbConnectionString, name: "ConfigurationDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{configurationTableName}]")
                    .AddSqlServer(persistedGrantsDbConnectionString, name: "PersistentGrantsDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{persistedGrantTableName}]")
                    .AddSqlServer(identityDbConnectionString, name: "IdentityDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{identityTableName}]")
                    .AddSqlServer(logDbConnectionString, name: "LogDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{logTableName}]")
                    .AddSqlServer(auditLogDbConnectionString, name: "AuditLogDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{auditLogTableName}]")
                    .AddSqlServer(dataProtectionDbConnectionString, name: "DataProtectionDb",
                                  healthQuery: $"SELECT TOP 1 * FROM dbo.[{dataProtectionTableName}]");
                    break;

                case DatabaseProviderType.PostgreSQL:
                    healthChecksBuilder
                    .AddNpgSql(configurationDbConnectionString, name: "ConfigurationDb",
                               healthQuery: $"SELECT * FROM \"{configurationTableName}\" LIMIT 1")
                    .AddNpgSql(persistedGrantsDbConnectionString, name: "PersistentGrantsDb",
                               healthQuery: $"SELECT * FROM \"{persistedGrantTableName}\" LIMIT 1")
                    .AddNpgSql(identityDbConnectionString, name: "IdentityDb",
                               healthQuery: $"SELECT * FROM \"{identityTableName}\" LIMIT 1")
                    .AddNpgSql(logDbConnectionString, name: "LogDb",
                               healthQuery: $"SELECT * FROM \"{logTableName}\" LIMIT 1")
                    .AddNpgSql(auditLogDbConnectionString, name: "AuditLogDb",
                               healthQuery: $"SELECT * FROM \"{auditLogTableName}\"  LIMIT 1")
                    .AddNpgSql(dataProtectionDbConnectionString, name: "DataProtectionDb",
                               healthQuery: $"SELECT * FROM \"{dataProtectionTableName}\"  LIMIT 1");
                    break;

                case DatabaseProviderType.MySql:
                    healthChecksBuilder
                    .AddMySql(configurationDbConnectionString, name: "ConfigurationDb")
                    .AddMySql(persistedGrantsDbConnectionString, name: "PersistentGrantsDb")
                    .AddMySql(identityDbConnectionString, name: "IdentityDb")
                    .AddMySql(logDbConnectionString, name: "LogDb")
                    .AddMySql(auditLogDbConnectionString, name: "AuditLogDb")
                    .AddMySql(dataProtectionDbConnectionString, name: "DataProtectionDb");
                    break;

                default:
                    throw new NotImplementedException($"Health checks not defined for database provider {databaseProviderConfiguration.ProviderType}");
                }
            }
        }
 private static void AddGenericHealthChecks(IHealthChecksBuilder healthChecksBuilder, string serviceName)
 {
     // Generic health checks, these will only be executed by the health metrics reporter.
     healthChecksBuilder.AddDbContextCheck <DataContext>($"{serviceName} database connectivity");
 }
Пример #12
0
        public static IHealthChecksBuilder AddNetCleanDbContextCheck(this IHealthChecksBuilder builder)
        {
            builder.AddDbContextCheck <DbContext>();

            return(builder);
        }
Пример #13
0
 /// <summary>
 /// Adds feature specific health checks.
 /// </summary>
 /// <param name="healthChecks">The health checks builder.</param>
 /// <returns>Health checks builder for further chaining.</returns>
 public static IHealthChecksBuilder AddFeatureHealthChecks(this IHealthChecksBuilder healthChecks)
 {
     healthChecks.AddDbContextCheck <PdsContext>();
     return(healthChecks);
 }