/// <summary>
        /// Initializes config locations and setup
        /// </summary>
        private void Initialize()
        {
            var parentMask  = pathMask;
            var searchPath  = CurrentPath;
            var configFiles = new List <string>();

            // Find RootPath
            if (File.Exists($@"{CurrentPath.AddLast(@"\")}{FileNames.AppSettingsJson}"))
            {
                RootPath = CurrentPath;
            }
            else
            {
                for (var count = 0; count < parentLevels; count++)
                {
                    searchPath = Path.GetFullPath(Path.Combine(CurrentPath, parentMask));
                    if (File.Exists($@"{searchPath.AddLast(@"\")}{FileNames.AppSettingsJson}"))
                    {
                        RootPath = searchPath;
                        break;
                    }
                    parentMask += pathMask;
                }
            }
            // Add appsettings.json
            if (File.Exists($@"{RootPath.AddLast(@"\")}{FileNames.AppSettingsJson}"))
            {
                configFiles.Add($@"{RootPath.AddLast(@"\")}{FileNames.AppSettingsJson}");
            }
            // Add appsettings.{environment}.json
            if (File.Exists($@"{RootPath.AddLast(@"\")}{string.Format(FileNames.AppSettingsEnvironmentJson, CurrentEnvironment)}"))
            {
                configFiles.Add($@"{RootPath.AddLast(@"\")}{string.Format(FileNames.AppSettingsEnvironmentJson, CurrentEnvironment)}");
            }
            // Add App_Data\AppSettings.json
            if (File.Exists($@"{AppDataFolder.AddLast(@"\")}{FileNames.AppSettingsJson}"))
            {
                configFiles.Add($@"{AppDataFolder.AddLast(@"\")}{FileNames.AppSettingsJson}");
            }
            // Add App_Data\ConnectionStrings.json
            if (File.Exists($@"{AppDataFolder.AddLast(@"\")}{FileNames.ConnectionStringsJson}"))
            {
                configFiles.Add($@"{AppDataFolder.AddLast(@"\")}{FileNames.ConnectionStringsJson}");
            }
            // Load
            Load(configFiles);
        }
Beispiel #2
0
        /// <summary>
        /// Loads from JSON data
        ///  Use: Configuration["subsection:suboption1"]
        /// </summary>
        public void Load()
        {
            IConfigurationRoot    configuration;
            IConfigurationBuilder configBuilder;
            var currentEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";

            configBuilder = new ConfigurationBuilder()
                            .AddJsonFile($@"{RootPath.AddLast(@"\")}{FileNames.AppSettingsJson}", optional: true)
                            .AddJsonFile($@"{RootPath.AddLast(@"\")}{string.Format(FileNames.AppSettingsEnvironmentJson, currentEnvironment)}");
            configuration = configBuilder.Build();

            foreach (var Item in configuration?.GetSection("AppSettings").GetChildren())
            {
                AppSettings.Add(new AppSettingSafe(Item.Key, Item.Value));
            }
            foreach (var Item in configuration.GetSection("ConnectionStrings").GetChildren())
            {
                ConnectionStrings.Add(new ConnectionStringSafe(Item.Key, Item.Value));
            }
        }