.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 ...
随机推荐
- JavaWeb面试篇(7)
61,JDBC访问数据库的基本步骤是什么?1,加载驱动2,通过DriverManager对象获取连接对象Connection3,通过连接对象获取会话4,通过会话进行数据的增删改查,封装对象5,关闭资源 ...
- Vue的watch和computed方法的使用
Vue的watch属性 Vue的watch属性可以用来监听data属性中数据的变化 <!DOCTYPE html> <html> <head> <meta c ...
- AOP前世与今生,aspect
AOP前世与今生 -------------------------------- 1.代码编写重复,(简单重构) 2.改善 3.静态代理(不改变原代码,继乘原来接口),代理类, aop 最原始出发点 ...
- 【学习小记】Berlekamp-Massey算法
Preface BM算法是用来求一个数列的最短线性递推式的. 形式化的,BM算法能够对于长度为n的有穷数列或者已知其满足线性递推的无穷数列\(a\),找到最短的长度为m的有穷数列\(c\),满足对于所 ...
- 【canvas学习笔记七】混合和裁剪
globalCompositeOperation 如果我们先画了一个图形,然后要在这个图形上面再画一个图形,那么这个图形会怎么样呢?是覆盖在原来的图形上面吗?这时候,就要用到globalComposi ...
- Python3学习笔记(九):赋值,浅拷贝和深拷贝区别
一.变量赋值 在Python可变数据类型(列表,字典,集合)中,把一个可变数据类型的变量赋给另一个变量,这两个变量引用的是同一个对象,内存地址是一样的,修改当中的一个变量,另一个变量相应也会被修改 & ...
- [BZOJ4804]欧拉心算:线性筛+莫比乌斯反演
分析 关于这道题套路到不能再套路了没什么好说的,其实发这篇博客的目的只是为了贴一个线性筛的模板. 代码 #include <bits/stdc++.h> #define rin(i,a,b ...
- 将Microsoft SQL Server 2000数据库转换成MySQL数据库
1. 下载并安装MyODBC.(如果是XP请下载5.3的旧版本,8.x的新版本运行有问题) 2. 创建一个空的MySQL数据库. 3. 在Windows >> 控制面板 >> ...
- java 解析上传的Excel文件
java poi解析上传的Excel文件 package com.zhl.push.Utils; /** * @Author TAO * @ClassName ExcelData * @Descrip ...
- 【mysql】如何通过navicat配置表与表的多对一关系,一对一关系?设计外键的效果
背景: 现在要将接口自动化测试结果持久化,当前只是每次运行接口测试,将测试结果通过邮件发送给项目组成员.邮件内容如下: 表设计: 为了呈现这个结果:我设计了2张表run_result和run_deta ...