public static String DbConnectionString(DbPlatforms dbPlatform) { string s = dbPlatform.ToString(); string connectionStringEnvironmentVariable = ENV_VAR_PREFIX + s + "_DB_CONNECTION_STRING"; string v = GetEnvironmentVariable(connectionStringEnvironmentVariable); if (string.IsNullOrEmpty(v)) { throw new Exception($"Environment variable {connectionStringEnvironmentVariable} not defined."); } return(v); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { DbPlatforms dbPlatform = DbPlatform(); string connection = DbConnectionString(dbPlatform); switch (dbPlatform) { case DbPlatforms.SQL_SERVER: services.AddDbContext <StrResDbContext>(options => options.UseSqlServer(connection, b => b.MigrationsAssembly("StrResApi"))); break; case DbPlatforms.SQLITE: services.AddDbContext <StrResDbContext>(options => options.UseSqlite(connection, b => b.MigrationsAssembly("StrResApi"))); break; default: throw new Exception("Unknown database platform"); } // services services.AddScoped <ITenantService, TenantService>(); services.AddScoped <IResourceService, ResourceService>(); services.AddScoped <IAdminService, AdminService>(); // repositories services.AddScoped <ITenantRepository, TenantRepository>(); services.AddScoped <IResourceRepository, ResourceRepository>(); services.AddScoped <IAdminRepository, AdminRepository>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddAuthentication(o => { o.DefaultScheme = STR_RES_AUTH_SCHEME; }).AddStrResAuth(STR_RES_AUTH_SCHEME, STR_RES_AUTH_SCHEME_DISPLAY_NAME, o => { }); }