.net core 学习小结之 配置介绍(config)以及热更新
- 命令行的配置
var settings = new Dictionary<string, string>{
{ "name","cyao"},
{"age",""}
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(settings)内存中获取appsettings
.AddCommandLine(args);
var configuration = builder.Build();
Console.WriteLine($"name:{configuration["name"]}");
Console.WriteLine($"age:{configuration["age"]}");
Console.ReadLine();
Console.ReadKey(); - json文件的配置
首先新建一个class.json的配置文件
{
"classNo": "1",
"gread": "1",
"student": [
{
"name": "cyao",
"age": 18
},
{
"name": "jaya",
"age": 19
}
]
}然后在configuration中注册
var builder = new ConfigurationBuilder()
.AddJsonFile("class.json")//json文件中获取appsettings
//.AddInMemoryCollection(settings)内存中获取appsettings
.AddCommandLine(args);
var configuration = builder.Build();
Console.WriteLine($"classNo:{configuration["classNo"]}");
Console.WriteLine($"gread:{configuration["gread"]}");
Console.WriteLine($"name:{configuration["student::name"]}");
Console.WriteLine($"age:{configuration["student::age"]}");
Console.WriteLine($"name:{configuration["student::name"]}");
Console.WriteLine($"age:{configuration["student::age"]}");
Console.ReadLine();
Console.ReadKey();
- 从配置文件文本到C#对象实例的映射-option与Bind
namespace JwtAuth
{
public class JwtSettings
{
///使用者
public string Issuer { get; set; }
///颁发者
public string Audience { get; set; }
///秘钥必须大于16个字符
public string SecretKey { get; set; }
} }json文件格式如下
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"JwtSettings": {
"Issuer": "http://locahost:5000",
"Audience": "http://locahost:5000",
"SecretKey": "hello world this is my key for cyao"
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; namespace JwtAuth
{
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Tokens; public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//将配置文件读取到settings
services.Configure<JwtSettings>(Configuration.GetSection("JwtSettings"));
JwtSettings settings = new JwtSettings();
Configuration.Bind("JwtSettings", settings);
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
} - 配置文件热更新
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3"/>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0"/>
</ItemGroup> - 框架设计:Configuration
.net core 学习小结之 配置介绍(config)以及热更新的更多相关文章
- 我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain 用于 热更新
大家好, 我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain 用于 热更新 . 简单的说, 原理就是 类似 Asp.net 那样 让 当前 WebApp 运行在一个 A ...
- .NET CORE学习笔记系列 开篇介绍
ASP.NET Core学习和使用了一段时间了,好记性不如烂笔头,通过查阅官网学习文档和一些大神们的博客总结一下.主要路线先总结一下ASP.NET Core的基础知识,然后是ASP.NET Core ...
- .net core 学习小结之环境配置篇
安装IIs对 netcore 的支持 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-mod ...
- SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...
- SpringCloud学习系列之四-----配置中心(Config)使用详解
前言 本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程. SpringCloud Config Config 介绍 Spring Clou ...
- SpringCloud全家桶学习之分布式配置中心----Config(七)
一.概述 (1)背景 微服务意味着将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中出现大量的服务.由于每个服务都需要配置必要的配置信息才能运行,所以一套集中式的.动态的配置管理 ...
- nopcommerce 4.1 core 学习 增加商城配置属性
需求: 原本是想用nop 来做国际版的商城,可以像亚马逊那样 国内外通用, 专门增加一个跨进元素属性. 学习里面的一些架构思想. 国内的行情还是 像himall 会比较实用. 这是在商城的综合 ...
- Asp.net core 学习笔记 ( Configuration 配置 )
参考 : https://cnblogs.com/nianming/p/7083964.html 配置写在 appsettings.json 里头 比如 { "object": { ...
- .net core 学习小结之 PostMan报415
首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝. 也就是说我所准备的数据格式并不是后台代码使用的数据格式 后台代码如下 using ...
随机推荐
- 描述GPT是什么?
介绍: 全球唯一标识分区表(GUID Partition Table,缩写:GPT)是一个实体硬盘的分区表的结构布局的标准.它是可扩展固件接口(UEFI)标准(被Inter用于代替个人计算机的BIOS ...
- Python修炼之路-异常
异常处理 在程序出现bug时一般不会将错误信息直接显示给用户,而是可以自定义显示内容或处理. 常见异常 AttributeError # 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性 ...
- JMeter 功能挖掘之 WEB 文件导出
前言 自从写从0构建自动化测试平台(一)之技术选型开始,在工作中Get新技能就非常想郑重的记录下来,方便自己查阅:相信很多人都有这种感触:平时问题解决后,没有及时记录,下次遇到类似问题,需要花同等的成 ...
- c++ copy和operator =
目录(?)[+] 构造函数 拷贝构造函数 赋值函数 C++中一般创建对象,拷贝或赋值的方式有构造函数,拷贝构造函数,赋值函数这三种方法.下面就详细比较下三者之间的区别以及它们的具体实现 1.构造函 ...
- python从入门到实践 第二章
python变量赋值: python的变量赋值 可以是单引号 也可以是双引号python 变量赋值的时候不能加()的 比如 name = "My Name is GF"变量赋值的时 ...
- jdk java 环境变量配置
下载 下载JDK 64 位 wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...
- [转] C++ 的关键字(保留字)完整介绍
转载至:https://www.runoob.com/w3cnote/cpp-keyword-intro.html 1. asm asm (指令字符串):允许在 C++ 程序中嵌入汇编代码. 2. a ...
- Codeforces 912E Prime Gift ( 二分 && 折半枚举 && 双指针技巧)
题意 : 给你 N ( 1 ≤ N ≤ 16 ) 个质数,然后问你由这些质数作为因子的数 ( 此数不超 10^18 ) & ( 不一定需要其因子包含所给的所有质数 ) 的第 k 个是什么 分析 ...
- Python3学习笔记(五):列表和元组
一.列表 列表是可变的--可以改变列表的内容 list函数可以把各种类型的序列拆分列表 >>> list('Hello') ['H', 'e', 'l', 'l', 'o'] 二.列 ...
- Spring Cloud云服务架构 - commonservice-config配置服务搭建
1. 介绍 Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与 ...