Aso.Net Core 的配置系统Configuration--转
Aso.Net Core 的配置系统Configuration
1.以前的配置文件格式为XML
2.新版的配置文件格式支持 { json ,xml, ini, memory, command, env..... }
01.Json文件的弱类型方式读取
Json文件 【Microsoft.Extensions.Configuration.Json】
添加配置文件json文件,并设置内容
{
"DataBase": {
"SqlServer": {
"ConnectionString": "server=.;database=testdb;uid=sa;pwd=123;"
},
"MySql": {
"IpAddress": "127.0.0.1",
"port": 3306
}
},
"endArray": [
{ "endId": 20 },
{ "endId": "30" }
]
}获取数据
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;
等等。
services.Configure<DataBase>(Configuration.GetSection("DataBase"));
这会将其设置为一个单例注入到容器中。
Aso.Net Core 的配置系统Configuration--转的更多相关文章
- Aso.Net Core 的配置系统Configuration
目录 Aso.Net Core 的配置系统Configuration 01.Json文件的弱类型方式读取 02.Json文件的强类型获取方式 Aso.Net Core 的配置系统Configurati ...
- .net core系列之《新一代的配置系统Configuration在支持多数据源,热更新,层级化方面代码快速实践》
在我们之前.Net Framework的项目中,配置文件是WebConfig或AppcConfig文件,而当我们想要添加我们自定义的节点时,还需要在这个文件中的section中定义我们自定义的节点,这 ...
- 重新整理 .net core 实践篇————配置系统之盟约[五]
前言 在asp .net core 中我们会看到一个appsettings.json 文件,它就是我们在服务中的各种配置,是至关重要的一部门. 不管是官方自带的服务,还是我们自己编写的服务都是用它来实 ...
- 重新整理 .net core 实践篇—————配置系统之军令状[七](配置文件)
前言 介绍一下配置系统中的配置文件,很多服务的配置都写在配置文件中,也是配置系统的大头. 正文 在asp .net core 提供了下面几种配置文件格式的读取方式. Microsoft.extensi ...
- 重新整理 .net core 实践篇—————配置系统之间谍[八](文件监控)
前言 前文提及到了当我们的配置文件修改了,那么从 configurationRoot 在此读取会读取到新的数据,本文进行扩展,并从源码方面简单介绍一下,下面内容和前面几节息息相关. 正文 先看一下,如 ...
- 重新整理 .net core 实践篇—————配置系统之强类型配置[十]
前言 前文中我们去获取value值的时候,都是通过configurationRoot 来获取的,如configurationRoot["key"],这种形式. 这种形式有一个不好的 ...
- 重新整理 .net core 实践篇————配置系统——军令(命令行)[六]
前言 前文已经基本写了一下配置文件系统的一些基本原理.本文介绍一下命令行导入配置系统. 正文 要使用的话,引入Microsoft.extensions.Configuration.commandLin ...
- 重新整理 .net core 实践篇—————配置系统之简单配置中心[十一]
前言 市面上已经有很多配置中心集成工具了,故此不会去实践某个框架. 下面链接是apollo 官网的教程,实在太详细了,本文介绍一下扩展数据源,和简单翻翻阅一下apollo 关键部分. apollo 服 ...
- .NET Core采用的全新配置系统[1]: 读取配置数据
提到“配置”二字,我想绝大部分.NET开发人员脑海中会立马浮现出两个特殊文件的身影,那就是我们再熟悉不过的app.config和web.config,多年以来我们已经习惯了将结构化的配置定义在这两个文 ...
随机推荐
- Java核心复习——CompletableFuture
介绍 JDK1.8引入CompletableFuture类. 使用方法 public class CompletableFutureTest { private static ExecutorServ ...
- 如何在虚拟机中安装kali linux
整理笔记,把以前印象笔记中记录的一些东西翻出来,想想发个随笔吧. 第一步在官网下载kali linux的镜像. 网址:https://www.kali.org/downloads/ (我的电脑是64位 ...
- 2018-2019-2 《网络对抗技术》Exp9 Web安全基础 20165114
Exp9 Web安全基础 目录 一.实验内容 二.基础问题回答 (1)SQL注入攻击原理,如何防御 (2)XSS攻击的原理,如何防御 (3)CSRF攻击原理,如何防御 三.实践过程记录 3.1 注入缺 ...
- JavaWeb基础知识
一.WEB基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web ...
- What is the difference between XSS and CSRF from their execution perspective?
What is the difference between XSS and CSRF from their execution perspective? https://www.quora.com/ ...
- Swift_IOS之提示框UIAlertController
import UIKit class ViewController: UIViewController ,UIActionSheetDelegate{ @IBAction func btn1(_ se ...
- IntelliJ IDEA中构建spring-boot项目
1. 打开 IDEA ,新建项目 2. 选择Spring Initializr 并在 Choose Initializr Service URL 填入 https://start.spring.io ...
- HTML5 地理位置定位API(3)
HTML5 地理位置定位实例 这篇文章主要为大家介绍了HTML5地理定位的方法,实例讲述了html5获取坐标完整实现过程, 并对比不同浏览器运行效果给出参考结果,需要的朋友可以参考下 本文实例讲述了h ...
- linux ubuntu 如何解决warning: no newline at end of file?
今天写了一段代码, 是在Windows下编辑的, 保存后放在linux系统下编译. gcc和cc都产生以下的警告: a.h:1:2: warning: no newline at end of fil ...
- 002-创建型-03-单例模式(Singleton)【7种】、spring单例及原理
一.概述 保证一个类仅有一个实例,并提供一个全局访问点 私有构造器.线程安全.延迟加载.序列化和反序列化安全.反射攻击 1.1.适用场景 1.在多个线程之间,比如servlet环境,共享同一个资源或者 ...