.net core读取配置信息代码段

            //新建一个读取配置文件的对象
            var builder = new ConfigurationBuilder()
                .SetBasePath( AppContext.BaseDirectory )
                .AddJsonFile( "appsettings.json", true, true );

            var configuration = builder.Build();

            //实体化一个services
            var services = new ServiceCollection();
            //将appsetting.json内的FtpCredentials信息配置到FtpConfiguration实体
            services.Configure( configuration.GetSection( "FtpCredentials" ) );
            services.AddOptions();

            //创建一个服务支持对象
            var serviceProvider = services.BuildServiceProvider();

            FtpConfiguration = serviceProvider.GetService>().Value;



            //如在构造方法内可这样使用
            private readonly FtpConfiguration FtpConfiguration;

            public Config(IOptions ftpConfiguration)
            {
                this.FtpConfiguration = ftpConfiguration.Value;
            }

 

你可能感兴趣的:(.Net,Core)