Пример #1
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            // Load Open Banking Connector configuration options
            OpenBankingConnectorSettings obcSettings = _obcSettingsProvider.GetSettings();

            // Ensure DB exists
            using IServiceScope scope   = _serviceScopeFactory.CreateScope();
            using BaseDbContext context = scope.ServiceProvider.GetRequiredService <BaseDbContext>();
            if (obcSettings.Database.ProcessedEnsureDbCreated)
            {
                // Create DB if configured to do so and DB doesn't exist
                context.Database.EnsureCreated();
            }
            else
            {
                // Throw exception if DB doesn't exist
                IRelationalDatabaseCreator creator = context.Database.GetService <IRelationalDatabaseCreator>();
                if (!creator.Exists())
                {
                    throw new ApplicationException(
                              "No database found. Run 'dotnet ef database update' in OpenBanking.WebApp.Connector.Sample root folder to create test DB.");
                }
            }

            return(Task.CompletedTask);
        }
Пример #2
0
        public static IServiceCollection AddGenericHostServices(
            this IServiceCollection services,
            IConfiguration configuration)
        {
            // Get settings via IOptions (ensure no updates after app start) and add to service collection
            services
            .Configure <OpenBankingConnectorSettings>(
                configuration.GetSection(new OpenBankingConnectorSettings().SettingsSectionName))
            .Configure <SoftwareStatementProfilesSettings>(
                configuration.GetSection(new SoftwareStatementProfilesSettings().SettingsSectionName))
            .AddOptions();
            services.AddSingleton <ISettingsProvider <OpenBankingConnectorSettings> >(
                sp =>
            {
                OpenBankingConnectorSettings obcSettings =
                    sp.GetRequiredService <IOptions <OpenBankingConnectorSettings> >().Value;
                return(new DefaultSettingsProvider <OpenBankingConnectorSettings>(obcSettings));
            });
            services.AddSingleton <ISettingsProvider <SoftwareStatementProfilesSettings> >(
                sp =>
            {
                SoftwareStatementProfilesSettings softwareStatementProfilesSettings =
                    sp.GetRequiredService <IOptions <SoftwareStatementProfilesSettings> >().Value;
                return(new DefaultSettingsProvider <SoftwareStatementProfilesSettings>(
                           softwareStatementProfilesSettings));
            });

            // Set up software statement cache
            services
            .AddSingleton <IReadOnlyRepository <SoftwareStatementProfileCached>,
                           SoftwareStatementProfileCache>();

            // Set up time provider
            services.AddSingleton <ITimeProvider, TimeProvider>();

            // Set up logging
            services.AddSingleton <IInstrumentationClient, LoggerInstrumentationClient>();

            // Set up API client not associated with software statement profile
            services.AddSingleton <IApiClient>(
                sp => new ApiClient(
                    sp.GetRequiredService <IInstrumentationClient>(),
                    new HttpClient(
                        new HttpRequestBuilder()
                        .CreateMessageHandler())));     // IHttpClientFactory no longer needed as SocketsHttpHandler now used by default

            // Set up mapper for API variants (different Open Banking standards)
            services.AddSingleton <IApiVariantMapper, ApiVariantMapper>();

            // Configure DB
            DatabaseOptions databaseOptions = configuration
                                              .GetSection(new OpenBankingConnectorSettings().SettingsSectionName)
                                              .Get <OpenBankingConnectorSettings>()
                                              .Validate()
                                              .Database;
            string connectionString = configuration.GetConnectionString(databaseOptions.ConnectionStringName) ??
                                      throw new ArgumentException("Database connection string not found.");

            switch (databaseOptions.ProcessedProvider)
            {
            case DbProvider.Sqlite:
                services
                // See e.g. https://jasonwatmore.com/post/2020/01/03/aspnet-core-ef-core-migrations-for-multiple-databases-sqlite-and-sql-server
                .AddDbContext <BaseDbContext, SqliteDbContext>(
                    options => { options.UseSqlite(connectionString); });
                break;

            default:
                throw new ArgumentException("Unknown DB provider", configuration["DbProvider"]);
            }

            // Configure DB service
            services.AddScoped <IDbService, DbService>();

            // Configure Request Builder
            services.AddScoped <IRequestBuilder, RequestBuilder>();

            // Startup tasks
            services.AddHostedService <StartupTasksHostedService>();

            return(services);
        }