using System.Collections.Concurrent; using Abp.Extensions; using Abp.Reflection.Extensions; namespace VberZero.Configuration; public static class AppConfigurations { private static readonly ConcurrentDictionary ConfigurationCache; static AppConfigurations() { ConfigurationCache = new ConcurrentDictionary(); } public static IConfigurationRoot Get(string path, string environmentName = null, bool addUserSecrets = false) { var cacheKey = path + "#" + environmentName + "#" + addUserSecrets; return ConfigurationCache.GetOrAdd( cacheKey, _ => BuildConfiguration(path, environmentName, addUserSecrets) ); } private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null, bool addUserSecrets = false) { var builder = new ConfigurationBuilder() .SetBasePath(path) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); if (!environmentName.IsNullOrWhiteSpace()) { builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true); } builder = builder.AddEnvironmentVariables(); if (addUserSecrets) { builder.AddUserSecrets(typeof(AppConfigurations).GetAssembly()); } return builder.Build(); } }