Ejemplo n.º 1
0
        /// <summary>
        /// Reads the configuration section from the appsettings.json file and deserializes it to the specified Settings type.
        /// The Configuration object is created read from based on the appsettings.json. The appsettings.json file name is determined by reading the ASPNETCORE_ENVIRONMENT variable i.e. appsettings.{environment}.json or appsettings.json when the environment variable is not set.
        /// The section name in the appsettings.json file is depetermined based on the name of the Settings type e.g. DatabaseSettings.
        /// </summary>
        public static S GetSettings <S>() where S : NKitSettings
        {
            string             appSettingsFileName  = NKitInformation.GetAspNetCoreEnvironmentAppSettingsFileName();
            IConfigurationRoot configurationBuilder = new ConfigurationBuilder().AddJsonFile(appSettingsFileName).Build();

            return(GetSettings <S>(configurationBuilder));
        }
        /// <summary>
        /// Register default Configurations from the appsettings.json which will be made available as IOptions to all services in the DI service container.
        /// </summary>
        /// <param name="services">The IServiceCollection passed into the ConfigureServices method in the Startup class.</param>
        /// <param name="configuration">The IConfiguration passed into the ConfigureServices method in the Startup class.</param>
        /// <param name="generalSettings">Returns the General settings that have been registered.</param>
        /// <param name="webApiSettings">Returns the Web API settings that have been registered.</param>
        /// <param name="httpExceptionHandlerMiddlewareSettings">Returns the HTTP Exception Handler Middler settings that have been registered.</param>
        /// <param name="webApiClientSettings">Returns the Web API Client settings that have been registered.</param>
        /// <param name="databaseSettings">Returns the Database settings that have been registered.</param>
        /// <param name="loggingSettings">Returns the Logging settings that have been registered.</param>
        /// <param name="emailSettings">Returns the Email settings that have been registered.</param>
        /// <param name="logger">Optional ILogger which if specified is used to log all the NKit settings read from the appsettings.json file.</param>
        public static void RegisterDefaultNKitSettings(
            this IServiceCollection services,
            IConfiguration configuration,
            out NKitGeneralSettings generalSettings,
            out NKitWebApiControllerSettings webApiSettings,
            out NKitHttpExceptionHandlerMiddlewareSettings httpExceptionHandlerMiddlewareSettings,
            out NKitWebApiClientSettings webApiClientSettings,
            out NKitDbContextSettings databaseSettings,
            out NKitLoggingSettings loggingSettings,
            out NKitEmailClientServiceSettings emailSettings,
            ILogger logger)
        {
            generalSettings = NKitGeneralSettings.RegisterConfiguration(configuration, services);
            webApiSettings  = NKitWebApiControllerSettings.RegisterConfiguration(configuration, services);
            httpExceptionHandlerMiddlewareSettings = NKitHttpExceptionHandlerMiddlewareSettings.RegisterConfiguration(configuration, services);
            webApiClientSettings = NKitWebApiClientSettings.RegisterConfiguration(configuration, services);
            databaseSettings     = NKitDbContextSettings.RegisterConfiguration(configuration, services);
            loggingSettings      = NKitLoggingSettings.RegisterConfiguration(configuration, services);
            emailSettings        = NKitEmailClientServiceSettings.RegisterConfiguration(configuration, services);
            if (logger == null)
            {
                return;
            }
            StringBuilder logMessage = new StringBuilder();

            logMessage.AppendLine($"*** Default NKit Settings registered from : {NKitInformation.GetAspNetCoreEnvironmentAppSettingsFileName()} ***");
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitGeneralSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(generalSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitWebApiControllerSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(webApiSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitHttpExceptionHandlerMiddlewareSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(httpExceptionHandlerMiddlewareSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitDbContextSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(databaseSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitLoggingSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(loggingSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitEmailClientServiceSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(emailSettings));
            logMessage.AppendLine();

            logMessage.AppendLine($"*** {nameof(NKitWebApiClientSettings)} ***");
            logMessage.AppendLine(GOC.Instance.JsonSerializer.SerializeToText(webApiClientSettings));
            logMessage.AppendLine();

            logger.LogInformation(logMessage.ToString());
        }
        /// <summary>
        /// Updates database to the latest migration on the given DbContext.
        /// </summary>
        public static void UpdateNKitDatabase <D>(this IApplicationBuilder applicationBuilder) where D : DbContext
        {
            NKitDbContextSettings dbContextSettings = NKitDbContextSettings.GetSettings();

            if (dbContextSettings == null)
            {
                throw new Exception($"Cannot update NKitDatabase when {nameof(NKitDbContextSettings)} have not been specified in the {NKitInformation.GetAspNetCoreEnvironmentAppSettingsFileName()} file.");
            }
            using (var serviceScope = applicationBuilder.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <D>())
                {
                    context.Database.Migrate();
                }
            }
        }