内容目录
主要特点:
1,根据当前环境加载 appsettings.xxx.json
文件,或者加载其它 .json
配置文件。
2,AddCommandLine
、AddEnvironmentVariables
从程序启动命令行参数和环境变量中导入配置。命令行参数需要以键值的形式填入,如 key1=value1 --key2=value2
。
public static IConfigurationRoot BuildConfiguration(
AbpConfigurationBuilderOptions options = null,
Action<IConfigurationBuilder> builderAction = null)
{
options = options ?? new AbpConfigurationBuilderOptions();
if (options.BasePath.IsNullOrEmpty())
{
options.BasePath = Directory.GetCurrentDirectory();
}
var builder = new ConfigurationBuilder()
.SetBasePath(options.BasePath)
.AddJsonFile(options.FileName + ".json", optional: true, reloadOnChange: true);
if (!options.EnvironmentName.IsNullOrEmpty())
{
builder = builder.AddJsonFile($"{options.FileName}.{options.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
if (options.EnvironmentName == "Development")
{
if (options.UserSecretsId != null)
{
builder.AddUserSecrets(options.UserSecretsId);
}
else if (options.UserSecretsAssembly != null)
{
builder.AddUserSecrets(options.UserSecretsAssembly, true);
}
}
builder = builder.AddEnvironmentVariables(options.EnvironmentVariablesPrefix);
if (options.CommandLineArgs != null)
{
builder = builder.AddCommandLine(options.CommandLineArgs);
}
builderAction?.Invoke(builder);
return builder.Build();
}
文章评论