public void ConfigureServices(IServiceCollection services) { var databaseEnvironment = new MyDatabaseEnvironment(Environment.GetEnvironmentVariable("DATABASE_ENVIRONMENT")); services.AddControllersWithViews(options => { options.Filters.Add <ApiModelStateCheckFilterAttribute>(); options.Filters.Add <ApiExceptionFilterAttribute>(); }).AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new DateTimeIsoConverter())) .ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddRazorPages(); if (databaseEnvironment.IsPostgreSQL()) { services.AddDbContext <MyDbContext, PostgreSqlDbContext>(ServiceLifetime.Scoped); services.AddIdentity <User, Role>().AddEntityFrameworkStores <PostgreSqlDbContext>(); services.AddIdentityServer().AddApiAuthorization <User, PostgreSqlDbContext>() .AddProfileService <ProfileService>(); } else if (databaseEnvironment.IsSQLite()) { services.AddDbContext <MyDbContext, SqliteDbContext>(ServiceLifetime.Scoped); services.AddIdentity <User, Role>().AddEntityFrameworkStores <SqliteDbContext>(); services.AddIdentityServer().AddApiAuthorization <User, SqliteDbContext>() .AddProfileService <ProfileService>(); } services.AddAuthentication().AddIdentityServerJwt().AddJwtBearer(); services.AddMemoryCache(); services.AddSignalR(); services.AddSingleton <IdentityGenerator>(); services.AddScoped <EntityErrorDescriber>(); services.Configure <IdentityGeneratorOptions>(x => x.InstanceTag = 1); services.Configure <LocalFileStorageServiceOptions>(x => x.RootDirectory = Configuration.GetValue <string>("FileUploadRootDirectory")); services.Configure <ApplicationSettings>(x => Configuration.GetSection("ApplicationSettings").Bind(x)); services.Configure <ApplicationEnvironment>(environment => environment.DatabaseEnvironment = databaseEnvironment); services.AddStores(databaseEnvironment).AddManagers(); services.AddMyHostedService(databaseEnvironment); services.AddTransient <IEmailSender, FakeEmailSender>(); services.AddScoped <OperationFilterAttribute>(); }
public static IServiceCollection AddStores(this IServiceCollection services, MyDatabaseEnvironment databaseEnvironment) { if (databaseEnvironment.IsPostgreSQL()) { services.AddSingleton <IApplicationEventQueries, PostgreSqlApplicationEventQueries>(); } else if (databaseEnvironment.IsSQLite()) { services.AddSingleton <IApplicationEventQueries, SqliteApplicationEventQueries>(); } services.AddScoped <IFileStore, FileStore>(); services.AddScoped <LocalFileStorageService>(); services.AddScoped <IEventStore, EventStore>(); services.AddScoped <IApplicationStore, ApplicationStore>(); return(services); }
public static IServiceCollection AddMyHostedService(this IServiceCollection services, MyDatabaseEnvironment databaseEnvironment) { services.AddSingleton <IEventStoreQueue, EventStoreQueue>(); services.AddSingleton <IEventQueue, EventQueue>(); services.AddSingleton <IMonitorSettingsUpdatingQueue, MonitorSettingsUpdatingQueue>(); services.AddHostedService <EventStoreBackgroundService>(); services.AddHostedService <EventPushBackgroundService>(); services.AddHostedService <MonitorSettingsBackgroundService>(); services.AddSingleton <IEventDbWriter, EventWriter>(); if (databaseEnvironment.IsPostgreSQL()) { services.AddSingleton <IEventCleaner, PostgreSqlEventCleaner>(); } else if (databaseEnvironment.IsSQLite()) { services.AddSingleton <IEventCleaner, SqliteEventCleaner>(); } return(services); }