Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            ConnectionStringsConfiguration conn = Configuration.GetSection(ConnectionStringsConfiguration.ConnectionStrings).Get <ConnectionStringsConfiguration>();

            services.AddEntityFrameworkSqlServer().AddDbContext <SampleDBContext>(options => options.UseSqlServer(conn.DefaultConnection));

            services.Configure <ConnectionStringsConfiguration>(Configuration.GetSection("ConnectionStrings"));

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapping());
            });

            services.AddSingleton(config.CreateMapper());

            services.AddRepositoryConfig()
            .AddDomainConfig()
            .AddProviderConfig();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(name: "v1", new OpenApiInfo {
                    Title = "LB Sample", Version = "v1"
                });
            });
        }
        /// <summary>
        /// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants, Identity and Logging
        /// Configure the connection strings in AppSettings.json
        /// </summary>
        /// <typeparam name="TConfigurationDbContext"></typeparam>
        /// <typeparam name="TPersistedGrantDbContext"></typeparam>
        /// <typeparam name="TLogDbContext"></typeparam>
        /// <typeparam name="TIdentityDbContext"></typeparam>
        /// <typeparam name="TAuditLoggingDbContext"></typeparam>
        /// <typeparam name="TDataProtectionDbContext"></typeparam>
        /// <typeparam name="TAuditLog"></typeparam>
        /// <param name="services"></param>
        /// <param name="connectionStrings"></param>
        /// <param name="databaseProvider"></param>
        /// <param name="databaseMigrations"></param>
        public static void RegisterDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext,
                                               TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(
            this IServiceCollection services,
            ConnectionStringsConfiguration connectionStrings,
            DatabaseProviderConfiguration databaseProvider,
            DatabaseMigrationsConfiguration databaseMigrations)
            where TIdentityDbContext : DbContext
            where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext
            where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext
            where TLogDbContext : DbContext, IAdminLogDbContext
            where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext <TAuditLog>
            where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext
            where TAuditLog : AuditLog
        {
            switch (databaseProvider.ProviderType)
            {
            case DatabaseProviderType.SqlServer:
                services.RegisterSqlServerDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations);
                break;

            case DatabaseProviderType.PostgreSQL:
                services.RegisterNpgSqlDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations);
                break;

            case DatabaseProviderType.MySql:
                services.RegisterMySqlDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(databaseProvider.ProviderType), $@"The value needs to be one of {string.Join(", ", Enum.GetNames(typeof(DatabaseProviderType)))}.");
            }
        }
        public AuditRepository(ConnectionStringsConfiguration settings)
        {
            _connectionStringsConfiguration = settings;

            var client   = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);

            _data = database.GetCollection <Audit_Data>(settings.AuditCollectionName);
        }
        public StudentRepository(ConnectionStringsConfiguration settings)
        {
            _connectionStringsConfiguration = settings;

            var client   = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);

            _students = database.GetCollection <Student_Data>(settings.StudentCollectionName);
        }
        private static IConnectionStringsConfiguration CreateConnectionStringsConfiguration(IComponentContext context)
        {
            var configuration = context.Resolve <IConfiguration>();

            var result = new ConnectionStringsConfiguration();

            new ConfigureFromConfigurationOptions <ConnectionStringsConfiguration>(configuration.GetSection("ConnectionStrings"))
            .Configure(result);

            return(result);
        }
Exemple #6
0
        internal static void AddPersistenceLayer(this IServiceCollection services, ConnectionStringsConfiguration _settings)
        {
            services.AddSingleton <ConnectionStringsConfiguration>(_settings);
            services.Configure <ConnectionStringsConfiguration>(option =>
            {
                option.StudentCollectionName = _settings.StudentCollectionName;
                option.ConnectionString      = _settings.ConnectionString;
                option.DatabaseName          = _settings.DatabaseName;
                option.AuditCollectionName   = _settings.AuditCollectionName;
            });

            services.AddTransient <IStudentRepository, StudentRepository>();
            services.AddTransient <IAuditRepository, AuditRepository>();
        }
        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}");
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants, Identity and Logging
        /// Configure the connection strings in AppSettings.json
        /// </summary>
        /// <typeparam name="TConfigurationDbContext"></typeparam>
        /// <typeparam name="TPersistedGrantDbContext"></typeparam>
        /// <typeparam name="TLogDbContext"></typeparam>
        /// <typeparam name="TIdentityDbContext"></typeparam>
        /// <typeparam name="TAuditLoggingDbContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="connectionStrings"></param>
        /// <param name="databaseMigrations"></param>
        public static void RegisterNpgSqlDbContexts <TIdentityDbContext, TConfigurationDbContext,
                                                     TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(this IServiceCollection services,
                                                                                                                                                           ConnectionStringsConfiguration connectionStrings,
                                                                                                                                                           DatabaseMigrationsConfiguration databaseMigrations)
            where TIdentityDbContext : DbContext
            where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext
            where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext
            where TLogDbContext : DbContext, IAdminLogDbContext
            where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext <TAuditLog>
            where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext
            where TAuditLog : AuditLog
        {
            var migrationsAssembly = typeof(DatabaseExtensions).GetTypeInfo().Assembly.GetName().Name;

            // Config DB for identity
            services.AddDbContext <TIdentityDbContext>(options =>
                                                       options.UseNpgsql(connectionStrings.IdentityDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.IdentityDbMigrationsAssembly ?? migrationsAssembly)));

            // Config DB from existing connection
            services.AddConfigurationDbContext <TConfigurationDbContext>(options =>
                                                                         options.ConfigureDbContext = b =>
                                                                                                      b.UseNpgsql(connectionStrings.ConfigurationDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.ConfigurationDbMigrationsAssembly ?? migrationsAssembly)));

            // Operational DB from existing connection
            services.AddOperationalDbContext <TPersistedGrantDbContext>(options => options.ConfigureDbContext = b =>
                                                                                                                b.UseNpgsql(connectionStrings.PersistedGrantDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.PersistedGrantDbMigrationsAssembly ?? migrationsAssembly)));

            // Log DB from existing connection
            services.AddDbContext <TLogDbContext>(options => options.UseNpgsql(connectionStrings.AdminLogDbConnection,
                                                                               optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminLogDbMigrationsAssembly ?? migrationsAssembly)));

            // Audit logging connection
            services.AddDbContext <TAuditLoggingDbContext>(options => options.UseNpgsql(connectionStrings.AdminAuditLogDbConnection,
                                                                                        optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminAuditLogDbMigrationsAssembly ?? migrationsAssembly)));

            // DataProtectionKey DB from existing connection
            if (!string.IsNullOrEmpty(connectionStrings.DataProtectionDbConnection))
            {
                services.AddDbContext <TDataProtectionDbContext>(options => options.UseNpgsql(connectionStrings.DataProtectionDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.DataProtectionDbMigrationsAssembly ?? migrationsAssembly)));
            }
        }
Exemple #9
0
 public TestRepository(SampleDBContext _dbcontext, IOptions <ConnectionStringsConfiguration> connStringConfig) : base(_dbcontext)
 {
     conn = connStringConfig.Value;
 }
Exemple #10
0
 public Startup(IConfiguration configuration)
 {
     _configuration              = configuration;
     _connectionStrings          = _configuration.GetSection(SettingsCommonNodeNames.ConnectionStringsConfiguration).Get <ConnectionStringsConfiguration>();
     _rabbitMqConnectionSettings = _configuration.GetSection(SettingsCommonNodeNames.RabbitMqConnectionSettings).Get <RabbitMqConnectionSettings>();
 }