private static IConfigurationRoot BuildConfiguration()
        {
            const string appConfigJsonName = "appsettings.json";

            var appConfigJsonPath = Path.Combine(BasePath, appConfigJsonName);

            if (!File.Exists(appConfigJsonPath))
            {
                JsonFileHelper.AddOrUpdateSection(appConfigJsonPath, nameof(AppSettings), new AppSettings());
            }

            var builder = new ConfigurationBuilder()
                          .SetBasePath(BasePath)
                          .AddJsonFile(appConfigJsonName)
                          .AddJsonFile("Resources/appsettings.custom.json", true)
                          .AddEnvironmentVariables();

            if (!string.IsNullOrEmpty(EnvironmentName))
            {
                builder.AddJsonFile($"appsettings.{EnvironmentName}.json", true);
            }

            if (IsDevelopment())
            {
                builder.AddUserSecrets <Program>();
            }
            return(builder.Build());
        }
Ejemplo n.º 2
0
        private static IConfigurationRoot BuildConfiguration()
        {
            const string appConfigJsonName = "appsettings.json";

            var appConfigJsonPath = Path.Combine(BasePath, appConfigJsonName);

            if (!File.Exists(appConfigJsonPath))
            {
                JsonFileHelper.AddOrUpdateSection(appConfigJsonPath, nameof(ReplacementOptions), new ReplacementOptions());
            }
            else
            {
                WriteLine($"{Environment.NewLine}是否重新生成 appsettings.json 文件? Y/任意键");
                if (ReadLine().Trim().ToLower() == "y")
                {
                    JsonFileHelper.AddOrUpdateSection(appConfigJsonPath, nameof(ReplacementOptions), new ReplacementOptions());
                }
            }

            ForegroundColor = ConsoleColor.Yellow;
            WriteLine($"请先配置程序目录下的 appsettings.json 文件。");

            ResetColor();
            WriteLine($"{Environment.NewLine}配置完成?任意键继续...");
            ReadKey();

            var builder = new ConfigurationBuilder()
                          .SetBasePath(BasePath)
                          .AddJsonFile(appConfigJsonName, false, true);

            return(builder.Build());
        }
Ejemplo n.º 3
0
        public void Update(Action <T> updateAction, bool reload = true)
        {
            JsonFileHelper.AddOrUpdateSection(_jsonFilePath, _sectionName, updateAction);

            if (reload)
            {
                _configuration?.Reload();
            }
        }
Ejemplo n.º 4
0
        public void Update(Action <T> updateAction, bool reload = true, JsonSerializerOptions serializerOptions = null)
        {
            JsonFileHelper.AddOrUpdateSection(_jsonFilePath, _sectionName, updateAction, serializerOptions ?? _defaultSerializerOptions);

            if (reload)
            {
                _configuration?.Reload();
            }
        }