.net core 学习小结之 配置介绍(config)以及热更新

摘要:
{configuration[“age”]}“);“student”:“age”:{configuration[“gread”]}”);{configuration[“student:age”]}”);usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Hosting;
    • 命令行的配置
       var settings = new Dictionary<string, string>{
                      { "name","cyao"},
                      {"age","18"}
                  };
                  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:0:name"]}");
            Console.WriteLine($"age:{configuration["student:0:age"]}");
            Console.WriteLine($"name:{configuration["student:1:name"]}");
            Console.WriteLine($"age:{configuration["student:1: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)以及热更新》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Learn 1-1 刻意练习amazeui学习笔记--css(基本样式4)--打印样式Print下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Springboot 整合Jersey

  在查看EurekaServer 服务端代码时偶然看到Jersey这个东西,简单记录下。 1. 简介   Jersey是一个RESTFUL请求服务JAVA框架,与常规的JAVA编程使用的struts框架类似,它主要用于处理业务逻辑层。   1.X的版本是sun公司提供的独立的jar包,在2.X版本中已经将jersey融合到JavaSE中,在javax.w...

Kerberos安装及使用

转载请注明出处:http://www.cnblogs.com/xiaodf/ 2. 安装 Kerberos2.1. 环境配置  安装kerberos前,要确保主机名可以被解析。   主机名 内网IP 角色 Vmw201 172.16.18.201 Master KDC Vmw202 172.16.18.202 Kerberos cl...

h5---链接

在标题下方加入如下代码实现链接下方下划线取消 <style> a:link {text-decoration: none;color:red;} a:visited {color:green;text-decoration:none;} a:hover  {color:blue;text-decoration:underline;} a:ac...

Linux命令--查看磁盘空间

今天用“web发布平台”发布测试的服务,两个节点中发现有一个节点没有发布成功,压测TPS始终上不去,排查后发现只有一个节点在打日志,另一个节点的服务进程都没有在运行,由此断定应该是没有发布成功,有点坑的是发布平台上的发布记录是SUCCESS的,打开发布日志后才看到有一个节点是发布失败的。从错误日志中发现报错的关键字“IO”,应该就是磁盘空间满了造成的,查看...

解决配置vim中文乱码的问题

解决linux下vim乱码的情况:(修改vimrc的内容) 全局的情况下:即所有用户都能用这个配置 文件地址:/etc/vimrc 在文件中添加: set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 如果只修改个...

使用mysqldump导入不成功解决方法

今天遇到个奇葩的问题,使用mysqldump导出数据库,命令如下:mysqldump -u root -p密码 数据库名 >/tmp/mysql_data.sql,在使用导入时,执行成功,显示 -- MySQL dump 10.13  Distrib 5.7.11, for Linux (x86_64)---- Host: localhost   ...