コード例 #1
0
        public static IServiceCollection AddXmlConfig <T>(this IServiceCollection services, Action <ConfigOption> options) where T : class
        {
            var option = new ConfigOption();

            options(option);

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), option.ConfigPath))
                                .AddXmlFile(option.ConfigFile, optional: false, reloadOnChange: true)
                                .Build();

            return(services.Configure <T>(option.Name, configuration));
        }
コード例 #2
0
        public static IConfiguration GetJsonConfig(Action <ConfigOption> options, out ConfigOption option)
        {
            option = new ConfigOption();
            options(option);

            var basePath = option.ConfigPath;

            if (!Path.IsPathRooted(basePath))
            {
                basePath = Path.Combine(Directory.GetCurrentDirectory(), basePath);
            }

            return(new ConfigurationBuilder()
                   .SetBasePath(basePath)
                   .AddJsonFile(option.ConfigFile, optional: option.Optional, reloadOnChange: true)
                   .Build());
        }