Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var appSettings = new AppConfigLoader(_config).LoadSettings();

            services.AddSingleton(appSettings);

            if (!appSettings.EnableLoggingEndpoint)
            {
                LogManager.Configuration.Variables["endpointLoggingLevel"] = "OFF";
                LogManager.ReconfigExistingLoggers();
            }

            _logger.LogInformation(appSettings.ToString());

            services.AddLogging(builder => { builder.AddConsole(); });

            services.AddProblemDetails(_environment.IsDevelopment());
            _logger.LogInformation($"Environment is Dev: {_environment.IsDevelopment()}");


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };
            });

            var encryptionSettings = new EncryptionConfigLoader(_config).LoadEncryptionConfig();

            services.AddSingleton(encryptionSettings);
            _logger.LogInformation(encryptionSettings.ToString());

            var appAuthSettings = new AuthApiConfigLoader(_config).LoadAuthConfig();

            services.AddSingleton(appAuthSettings);

            _logger.LogInformation(appAuthSettings.ToString());

            services.AddSingleton <IAuthHttpClient, AuthHttpClient>();

            services.AddScoped <IPracticeProvider>(sp => new PracticeProvider(appSettings.ConnectionString));
            services.AddScoped <IPracticeEnvironmentCache, PracticeEnvironmentCache>();

            //runtime resolution via factory:  https://stackoverflow.com/questions/37744637/how-can-i-pass-a-runtime-parameter-as-part-of-the-dependency-resolution
            var fileStreamLogger = services.BuildServiceProvider().GetService <ILogger <FileStreamProvider> >();

            services.AddTransient <IStreamProvider, FileStreamProvider>((sp) => new FileStreamProvider(fileStreamLogger, "", "version.txt"));

            services.AddTransient <IApplicationAuthService, ApplicationAuthService>();
            services.AddTransient <ValidateTokenFilter>();

            services.AddTransient((serviceProvider) =>
            {
                return(new Func <string, IClinicProvider>((env) => new DataAccess.GenPro.ClinicProvider(env, appSettings.ConnectionString)));
            });

            services.AddTransient((serviceProvider) =>
            {
                return(new Func <string, ITeamProvider>((env) => new TeamProvider(appSettings.ConnectionString)));
            });

            ConfigureSwagger(services, appSettings.ApplicationVersion);
            ConfigureAuth(services, appSettings.ClaimsSettingsModel);
        }