/// <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());
        }
Example #2
0
        /// <summary>
        /// Registers the NKitDbDbContext specified by D and the NKitDbContextRepository specified by R as transient services,
        /// which make them accessible through the Dependency Injection container and available to all NKit controllers just as the NKitWebApiController and/or NKitWebApiControllerCrud.
        /// </summary>
        /// <typeparam name="D">The Entity Framework NKitDbContext to be used to manage the database.</typeparam>
        /// <param name="services">The DI services container received in the Startup class.</param>
        /// <param name="configuration">The IConfiguration received in the Startup class.</param>
        public static void RegisterNKitDbContext <D>(
            this IServiceCollection services,
            IConfiguration configuration) where D : NKitDbContext
        {
            NKitDbContextSettings dbContextSettings = NKitDbContextSettings.GetSettings(configuration);

            if (dbContextSettings != null)
            {
                services.AddDbContext <D>(ServiceLifetime.Transient); //Register the NKitDbContext.
            }
        }
        /// <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();
                }
            }
        }
        /// <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="loggerCategoryName">Optional category name of the ILogger that will be created to log the settings being registered.</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,
            string loggerCategoryName)
        {
            ILogger logger = !string.IsNullOrEmpty(loggerCategoryName) ? NKitLoggingHelper.CreateLogger(loggerCategoryName, NKitLoggingSettings.GetSettings(configuration)) : null;

            RegisterDefaultNKitSettings(services, configuration,
                                        out generalSettings,
                                        out webApiSettings,
                                        out httpExceptionHandlerMiddlewareSettings,
                                        out webApiClientSettings,
                                        out databaseSettings,
                                        out loggingSettings,
                                        out emailSettings,
                                        logger);
        }