.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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值