.net core 选项方式读取.json配置文件

就个人而言更喜欢手动配置
.json配置文件内容

{
	"DB": {
	   "DBType": "SqlServer",
	   "DBName": "DemoDB",
	   "DBUserName": "localhost",
	   "DBPassword": "123456"
	 },
	 "Smtp": {
	   "Server": "127.0.0.1",
	   "UserName": "admin",
	   "Passwrod": "000000"
	 }
}

建立配置文件对应模型类(这里模型类的搭建重写了ToString()方法为的是更好的呈现效果)

namespace ConsoleApp1
{
    public class DbSettings
    {
        public string DBType { get; set; }

        public string DBName { get; set; }

        public string DBUserName { get; set; }

        public string DBPassword { get; set; }

        public override string ToString()
        {
            return $"DBType:{DBType}, DBName:{DBName}, DBUserName:{DBUserName}, DBPassword:{DBPassword}";
        }
    }
}
namespace ConsoleApp1
{
    public class StmpSettings
    {
        public string Server { get; set; }

        public string UserName { get; set; }

        public string Passwrod { get; set; }

        public override string ToString()
        {
            return $"Server:{Server}, UserName:{UserName}, Passwrod:{Passwrod}";
        }
    }
}

读取配置文件类

using Microsoft.Extensions.Options;

namespace ConsoleApp1
{
    public class ReadConfigClass
    {
    	//IOptions、IOptionsMonitor、IOptionsSnapshot
    	//三者就别在于,IOptions不监听,IOptionsMonitor更新配置时读取新的值,IOptionsSnapshot在同一个范围内即时更新配置中的值也读取的是旧值(为了避免在两处运行代码之间修改了值,一个已经读取一个还未读取,造成数据误差)
        private readonly IOptionsSnapshot<DbSettings> dbSanpshot;
        private readonly IOptionsSnapshot<StmpSettings> stmpSanpshot;

        public ReadConfigClass(IOptionsSnapshot<DbSettings> dbSanpshot, IOptionsSnapshot<StmpSettings> stmpSanpshot)
        {
            this.dbSanpshot = dbSanpshot;
            this.stmpSanpshot = stmpSanpshot;
        }

        public void test()
        {
            DbSettings dbValue = dbSanpshot.Value;
            StmpSettings stmpValue = stmpSanpshot.Value;

            Console.WriteLine($"DB:{dbValue.ToString()}");
            Console.WriteLine($"Stmp:{stmpValue.ToString()}");
        }
    }
}

依赖注入,服务(引用的对象)注入到容器

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp1
{
    public class DIReadConfig
    {
        public DIReadConfig()
        {
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("config.json", optional: false, reloadOnChange: true);

            IConfigurationRoot configurationRoot = configurationBuilder.Build();

            ServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddOptions()
                .Configure<DbSettings>(c => configurationRoot.GetSection("DB").Bind(c))
                .Configure<StmpSettings>(c => configurationRoot.GetSection("Stmp").Bind(c));

            serviceCollection.AddTransient<ReadConfigClass>();

            ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            ReadConfigClass readConfigClass = serviceProvider.GetRequiredService<ReadConfigClass>();

            readConfigClass.test();
        }
    }
}

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            new DIReadConfig();
        }
    }
}

你可能感兴趣的:(.NET,Core学习,.netcore,json)