【ABP 源码学习】配置启动源码

2022年6月24日 1822点热度 1人点赞 0条评论
内容目录

主要特点:
1,根据当前环境加载 appsettings.xxx.json 文件,或者加载其它 .json 配置文件。
2,AddCommandLineAddEnvironmentVariables 从程序启动命令行参数和环境变量中导入配置。命令行参数需要以键值的形式填入,如 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();
    }

痴者工良

高级程序员劝退师

文章评论