ASP .NET Core 配置系统

2018-11-28  本文已影响0人  无为无味无心

使用

VS2017 新建 Core Console程序,安装包

 Microsoft.Extensions.Configuration
 Microsoft.Extensions.Configuration.Json

项目下新建appsettings-dev.json 文件

{
  "sql": {
    "ip": "127.0.0.1",
    "port": 3389
  },
  "redis": {
    "ip": "127.0.0.1",
    "port": 6379
  }
}

使用代码

class Program
    {
        static void Main(string[] args)
        {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            //建造者模式
          var configuaration=  builder
              .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings-dev.json").Build();

            var sqlInfo= $"sql:{configuaration["sql:ip"]} :{configuaration["sql:port"]}";
            Console.WriteLine(sqlInfo);
                Console.ReadKey();
        }
    }

输出

sql:127.0.0.1 :3389

1 配置数据源多支持

json

xml

项目新建 appsettings.xml 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<appsettings>
    <ip>192.168.50.1</ip>
    <port>3398</port>
</appsettings>

项目 <span style="color:red">NUGET</span>安装 Microsoft.Extensions.Configuration.Xml
读取配置代码

var configuaration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
.AddXmlFile("appsettings.xml")
.Build();
var mysqlInfo = $"mysql:{configuaration["ip"]} :{configuaration["port"]}";
Console.WriteLine(mysqlInfo);
Console.ReadLine();

输出如下

mysql:192.168.50.1 :3398

ini

项目下新建 appsettings.ini 配置文件

[UserService]
name="UserService"
ip="192.168.1.101"

项目 <span style="color:red">NUGET</span>安装 Microsoft.Extensions.Configuration.Ini

读取配置的示例代码如下:

var configuaration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
  .AddIniFile("appsettings.ini")
    .Build();
var UserService = $"UserService Name:{configuaration["UserService:name"]} ;ip:{configuaration["UserService:ip"]}";
Console.WriteLine(UserService);

memory

读取示例代码如下:

var  data=new Dictionary<string,string>();
data.Add("UserName", "admin ");
data.Add("Pwd","123456");
var configuaration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
 .AddInMemoryCollection(data)
    .Build();
var user = $"$u:{configuaration["UserName"]},p:{configuaration["Pwd"]}";
Console.WriteLine(user);
Console.ReadLine();

command

env(系统变量)

项目 <span style="color:red">NUGET</span>安装 Microsoft.Extensions.Configuration.EnvironmentVariables

可以读取当前操作系统下的系统变量,读取示例代码如下:

var configuaration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
 .AddEnvironmentVariables()
  .Build();
 Console.WriteLine(configuaration["Path"]);//Path 为当前操作系统变量
 var p=   Environment.GetEnvironmentVariable("Path"); //也可以读取系统环境变量

2 热加载

reloadOnChange 参数设置为true

var configuaration = builder
.SetBasePath(Environment.CurrentDirectory)
      .AddJsonFile("appsettings-dev.json",true,reloadOnChange:true)//reloadOnChange
.Build();
var sqlInfo = $"sql:{configuaration["sql:ip"]} :{configuaration["sql:port"]}";
while (true)
{
Console.WriteLine(sqlInfo);
System.Threading.Thread.Sleep(1000);
}
         

3 多环境加载 (Development, Test,Fat,Production)

4 配置加载优先级

项目中加入两个配置文件

appsettings-dev.json

{
  "sql": {
    "ip": "127.0.0.1",
    "port": 3389
  },
  "redis": {
    "ip": "127.0.0.1",
    "port": 6379
  },
  "UserService": {
    "name": "UserService",
    "ip": "212.10.20.101"
  }
}

appsettings.json

{
  "UserService": {
    "name": "UserService",
    "ip": "127.0.0.1"
  }
}

读取配置代码如下:

 ConfigurationBuilder builder = new ConfigurationBuilder();
 var configuaration = builder
    .SetBasePath(Environment.CurrentDirectory)
    .AddJsonFile("appsettings-dev.json")
    .AddJsonFile("appsettings.json")
    .
    Build();
var UserService = $"UserService: ip :{configuaration["UserService:ip"]}";
Console.WriteLine(UserService);
Console.ReadLine();

输出为UserService: ip :127.0.0.1
证明了配置的加载完后,相同的配置 会被后加载的配置覆盖。(具体在源码中可以知道 加载完的配置被反转,然后读取到了就会返回)

5 配置强类型读取与弱类型读取

弱类型读取 代码示例

 //弱类型读取 使用 : 运算符
    Console.WriteLine($"UserService: ip :{configuaration["UserService:ip"]}");
    //弱类型读取  GetSection
    Console.WriteLine($"UserService: ip :{configuaration.GetSection("UserService")["ip"]}");

强类型读取
项目 <span style="color:red">NUGET</span> 安装 Microsoft.Extensions.Configuration.Binder

代码示例

//强类型读取 GetValue
Console.WriteLine($"UserService: ip :{configuaration.GetValue<string>("UserService:ip")}");
//强类型读取 Bind
ConfigRoot s = new ConfigRoot();
configuaration.Bind(s);
Console.WriteLine($"UserService: ip :{s.UserService.ip}");
// 强类型读取 Get<T>
Console.WriteLine($"UserService: ip :{ configuaration.Get<ConfigRoot>().UserService.ip}");
``

上一篇下一篇

猜你喜欢

热点阅读