zoukankan      html  css  js  c++  java
  • .NET Core 配置文件

    老一代配置系统

    1,XML格式 格式单一。

    2,配置信息Kye,只能一维化配置

    3,框架信息和应用程序信息混合到一起

     

    应用程序中是  App.config

    web项目中           web.config

    使用程序集  System.Configuration

    新一代配置系统

    1支持配置文件格式 json  xml  init  环境变量  memory

    2参数热加载

    3参数多维

    nuget包

    Microsoft.Extensions.Configuration

    主要是用上面的

    下面的是对应不同文件类型的扩展   都是扩展方法

    Microsoft.Extensions.Configuration.Json

    Microsoft.Extensions.Configuration.Xml

    Microsoft.Extensions.Configuration.Init

    Microsoft.Extensions.Configuration.EnvironmentVariables

    下面的是进行模型转换的

    Microsoft.Extensions.Configuration.Binder

    数据读取

    //1   :  运算符  层级管理  数据的话用索引
    var dcf1 = config["mysql:host"];
    Console.WriteLine(dcf1);
    var dcf11 = config["shopidlist:1:entid"];
    Console.WriteLine(dcf11);
    
    
    //2   getsection              getsection("")[""]
    var dcf2 = config.GetSection("mysql").GetSection("host").Value;
    Console.WriteLine(dcf2);
    var dcf21 = config.GetSection("mysql:host").Value;
    Console.WriteLine(dcf21);
    var dcf3 = config.GetSection("mysql")["host"];
    Console.WriteLine(dcf3);
    var dcf4 = config.GetSection("shopidlist").GetSection("1")["entid"];
    Console.WriteLine(dcf4);
    var dcf5 = config.GetSection("shopidlist").GetSection("1").GetSection("entid").Value;
    Console.WriteLine(dcf5);
    //3强类型读取       Microsoft.Extensions.Configuration.Binder
    //开发中常用强类型
    var dcf6 = config.GetValue<int>("shopidlist:1:entid");
    Console.WriteLine(dcf6);
    
    Rootobject dcf7 = new Rootobject();
    config.Bind(dcf7);
    Console.WriteLine(dcf7.mysql.host);
    View Code

    弱类型方式读取

    <1>: ':'运算符 mysql:host

    IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
    .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
    //.AddXmlFile("appsettings.xml")

    //.AddEnvironmentVariables()
    .Build();

    var info = configuration["shopidlist:0:entid"];


    {
    "mysql": {
    "host": "192.168.23.1",
    "port": 3306
    },
    "shopidlist": [
    { "entid": 20 },
    { "entid": 25 }
    ]
    }

    <2> GetSection (不同的人,不同的使用习惯) getSection("mysql")["host"]


    //var info = configuration.GetSection("shopidlist").GetSection("1").GetSection("entid").Value;
    var info = configuration.GetSection("shopidlist").GetSection("1")["entid"];


    public IConfigurationSection GetSection(string key)
    {
    string[] textArray1 = new string[] { this.Path, key };
    return this._root.GetSection(ConfigurationPath.Combine(textArray1));
    }


    强类型方式读取 : Microsoft.Extensions.Configuration.Binder


    <1> GetValue

    var info = configuration.GetValue<int>("mysql:port");


    public static object GetValue(this IConfiguration configuration, Type type, string key, object defaultValue)
    {
    string str = configuration.GetSection(key).Value;
    if (str != null)
    {
    return ConvertValue(type, str);
    }
    return defaultValue;
    }

    <2> 配置映射为实体类Bind,Get<T>

    Rootobject rootobject = new Rootobject();

    configuration.Bind(rootobject);

    Configuration.GetSection("Position").Get<PositionOptions>();

    StartUp类中
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<PositionOptions>(Configuration.GetSection("Position"));
        services.AddRazorPages();
    }
    
    其他类注入
    public class Test2Model : PageModel
    {
        private readonly PositionOptions _options;
    
        public Test2Model(IOptions<PositionOptions> options)
        {
            _options = options.Value;
        }
    
        public ContentResult OnGet()
        {
            return Content($"Title: {_options.Title} 
    " +
                           $"Name: {_options.Name}");
        }
    }

    代码下载

  • 相关阅读:
    SpringMvc
    Spring-Aop
    Spring-IOC
    Spring模块划分
    队列
    稀疏数组
    数据结构
    Nginx配置实例
    Nginx常用命令
    视频断点播放:h5+jquery
  • 原文地址:https://www.cnblogs.com/wudequn/p/10106041.html
Copyright © 2011-2022 走看看