Aso.Net Core 的配置系统Configuration

​ 1.以前的配置文件格式为XML

​ 2.新版的配置文件格式支持 { json ,xml, ini, memory, command, env..... }

01.Json文件的弱类型方式读取

  1. Json文件 【Microsoft.Extensions.Configuration.Json】

  2. 添加配置文件json文件,并设置内容

    {
    "DataBase": {
    "SqlServer": {
    "ConnectionString": "server=.;database=testdb;uid=sa;pwd=123;"
    },
    "MySql": {
    "IpAddress": "127.0.0.1",
    "port": 3306
    }
    },
    "endArray": [
    { "endId": 20 },
    { "endId": "30" }
    ]
    }
  3. 获取数据

    private static void Main(string[] args)
    {
    IConfiguration configuration = new ConfigurationBuilder()
    .SetBasePath(Environment.CurrentDirectory)
    .AddJsonFile("AppSettings.json",true,true)
    .AddInMemoryCollection()
    .Build();
    var str1 = configuration["DataBase:SqlServer:ConnectionString"];
    var str2 = configuration["endArray:0:endId"];
    Console.WriteLine($"{str1}+---{str2}");
    }

02.Json文件的强类型获取方式

1.添加引用【Microsoft.Extensions.Configuration.Binder】

2.GetValue方式获取

private static void Main(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("AppSettings.json",true,true)
.AddInMemoryCollection()
.Build(); //GetValue获取方式
int val = configuration.GetValue<int>("endArray:0:endId");
Console.WriteLine(val);
}

3.实体映射方式private static void Main(string[] args)

{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("AppSettings.json",true,true)
.AddInMemoryCollection()
.Build(); //Bind 获取方式
Root root=new Root();
configuration.Bind(root);
var ip = root.DataBase.MySql.IpAddress;
var entid = root.endArray[].endId;
Console.WriteLine($"{ip}{entid}");
//Get<T> 获取
var root = configuration.Get<Root>();
var ip = root.DataBase.MySql.IpAddress;
var entid = root.endArray[].endId;
Console.WriteLine($"{ip}{entid}"); }
//这里是实体类
public class SqlServer
{
/// <summary>
///
/// </summary>
public string ConnectionString { get; set; }
} public class MySql
{
/// <summary>
///
/// </summary>
public string IpAddress { get; set; }
/// <summary>
///
/// </summary>
public int port { get; set; }
} public class DataBase
{
/// <summary>
///
/// </summary>
public SqlServer SqlServer { get; set; }
/// <summary>
///
/// </summary>
public MySql MySql { get; set; }
} public class EndArray
{
/// <summary>
///
/// </summary>
public int endId { get; set; }
} public class Root
{
/// <summary>
///
/// </summary>
public DataBase DataBase { get; set; }
/// <summary>
///
/// </summary>
public List<EndArray> endArray { get; set; }
}

补充:IConfiguration.Get<T>()即可获得实体,所以我们无需从根获取起,如获取SQL的连接串,也可以这样写:

1、configuration.GetSection("DataBase").Get<DataBase>().SqlServer.ConnectionString;

2、configuration.GetSection("DataBase").GetSection("SqlServer").Get<SqlServer>().ConnectionString;

等等。

 
针对经常要使用的情形,建议将其纳入IOC
services.Configure<DataBase>(Configuration.GetSection("DataBase"));

这会将其设置为一个单例注入到容器中。

 

Aso.Net Core 的配置系统Configuration--转的更多相关文章

  1. Aso.Net Core 的配置系统Configuration

    目录 Aso.Net Core 的配置系统Configuration 01.Json文件的弱类型方式读取 02.Json文件的强类型获取方式 Aso.Net Core 的配置系统Configurati ...

  2. .net core系列之《新一代的配置系统Configuration在支持多数据源,热更新,层级化方面代码快速实践》

    在我们之前.Net Framework的项目中,配置文件是WebConfig或AppcConfig文件,而当我们想要添加我们自定义的节点时,还需要在这个文件中的section中定义我们自定义的节点,这 ...

  3. 重新整理 .net core 实践篇————配置系统之盟约[五]

    前言 在asp .net core 中我们会看到一个appsettings.json 文件,它就是我们在服务中的各种配置,是至关重要的一部门. 不管是官方自带的服务,还是我们自己编写的服务都是用它来实 ...

  4. 重新整理 .net core 实践篇—————配置系统之军令状[七](配置文件)

    前言 介绍一下配置系统中的配置文件,很多服务的配置都写在配置文件中,也是配置系统的大头. 正文 在asp .net core 提供了下面几种配置文件格式的读取方式. Microsoft.extensi ...

  5. 重新整理 .net core 实践篇—————配置系统之间谍[八](文件监控)

    前言 前文提及到了当我们的配置文件修改了,那么从 configurationRoot 在此读取会读取到新的数据,本文进行扩展,并从源码方面简单介绍一下,下面内容和前面几节息息相关. 正文 先看一下,如 ...

  6. 重新整理 .net core 实践篇—————配置系统之强类型配置[十]

    前言 前文中我们去获取value值的时候,都是通过configurationRoot 来获取的,如configurationRoot["key"],这种形式. 这种形式有一个不好的 ...

  7. 重新整理 .net core 实践篇————配置系统——军令(命令行)[六]

    前言 前文已经基本写了一下配置文件系统的一些基本原理.本文介绍一下命令行导入配置系统. 正文 要使用的话,引入Microsoft.extensions.Configuration.commandLin ...

  8. 重新整理 .net core 实践篇—————配置系统之简单配置中心[十一]

    前言 市面上已经有很多配置中心集成工具了,故此不会去实践某个框架. 下面链接是apollo 官网的教程,实在太详细了,本文介绍一下扩展数据源,和简单翻翻阅一下apollo 关键部分. apollo 服 ...

  9. .NET Core采用的全新配置系统[1]: 读取配置数据

    提到“配置”二字,我想绝大部分.NET开发人员脑海中会立马浮现出两个特殊文件的身影,那就是我们再熟悉不过的app.config和web.config,多年以来我们已经习惯了将结构化的配置定义在这两个文 ...

随机推荐

  1. Java 学习资料网站集合

    一.开源项目的搜集 https://www.jianshu.com/p/6c75174e0f07 -- https://github.com/flyleft/tip 二.简单的开源项目 https:/ ...

  2. 从xml中返回的对象,和new 返回的对象时不同的。

    public BigDecimal getTax() { return tax == null ? BigDecimal.ZERO : tax; } 这是自定义的一个类 对null 做出了处理. 但是 ...

  3. numpy模块-渐入佳境

    1.多维数组降为一维: numpy中的ravel().flatten().squeeze()的用法与区别 2. axis的理解 Python之NumPy(axis=0/1/2...)的透彻理解——通过 ...

  4. OpenVirtex安装

    目录 环境 安装 环境 我使用的java以及maven版本如下: jdk7下载地址:https://www.oracle.com/technetwork/java/javase/downloads/j ...

  5. Mac下安装php-memcached扩展

    [libmemcached安装] libmemcached可以通过直接下载后解压也可以采用wget下载 先下载libmemcached:方式一:libmemcached下载地址:https://lau ...

  6. StyleCop学习笔记-文档规则

    文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...

  7. Inventor2018专业版软件安装激活教程

    如果你安装的是Autodesk Inventor Professional 2018,那么序列号为:666-69696969,产品密钥为:797J1, 如果你安装的是Autodesk Inventor ...

  8. Linux/CentOS下修改MAC地址

    Linux/CentOS下修改MAC地址 摘自:https://blog.csdn.net/qq_33233768/article/details/64906265 2017年03月22日 11:06 ...

  9. Java测试当前应用所占用的内存示例

    package test; import java.util.HashMap; import java.util.Map; public class TestMemory { public stati ...

  10. react中异步的使用

    let promise; promise = this.props.corporationService.preSearchPage(params); promise.then((data) => ...