예제 #1
0
        /// <summary>
        /// 添加本地配置
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="contentRoot"></param>
        /// <param name="environmentName"></param>
        /// <param name="customConfigJsonFile">自定义配置目录</param>
        /// <param name="args">启动参数</param>
        /// <param name="argsConfigJsonFile">参数指定配置目录</param>
        /// <param name="endDir">递归向上查找文件的结束目录</param>
        public static IConfigurationBuilder AddLocalConfiguration(this IConfigurationBuilder builder, string contentRoot, string environmentName, string customConfigJsonFile, string[] args, string argsConfigJsonFile, DirectoryInfo?endDir)
        {
            // 默认配置文件
            builder.AddDefaultConfigFile(contentRoot, environmentName, endDir);

            // config配置
            builder.AddJsonFile("config.json", true, true);
            builder.AddIniFile("config.ini", true, true);

            builder.AddJsonFile($"config.{environmentName}.json", true, true);
            builder.AddIniFile($"config.{environmentName}.ini", true, true);

            builder.AddJsonFile($"config.{Environment.MachineName}.json", true, true);
            builder.AddIniFile($"config.{Environment.MachineName}.ini", true, true);

            builder.AddJsonFile($"config.{Environment.UserName}.json", true, true);
            builder.AddIniFile($"config.{Environment.UserName}.ini", true, true);

            // 添加 secrets 目录配置文件
            builder.AddDirFiles(Path.Combine(contentRoot, secrets));

            // 添加 config 目录配置文件
            builder.AddDirFiles(Path.Combine(contentRoot, config));

            // 自定义配置文件
            if (!string.IsNullOrEmpty(customConfigJsonFile))
            {
                builder.AddJsonFile(customConfigJsonFile, true, true);
            }
            if (args != null)
            {
                // 参数指定配置文件
                if (!string.IsNullOrEmpty(argsConfigJsonFile))
                {
                    builder.AddJsonFile(argsConfigJsonFile, true, true);
                }
                builder.AddCommandLine(args);
            }
            return(builder);
        }