zoukankan      html  css  js  c++  java
  • asp.net core 读取 appsettings.json 节点值

    方法一直接读取JSON节点值:

      "AppSettings": {
    "CacheDays": 30,
    }
    public string GetIntelligenceApiUrl
            {
                get
                {
                    var cacheDays = new ConfigurationHelper().Get<string>("AppSettings:CacheDays");
                    return cacheDays;
                }
            }

    方法一的操作类:

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.Json;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Options;
    using System.IO;
    
        /// <summary>
        /// 读取配置文件
        /// </summary>
        public class ConfigurationHelper
        {
            public IConfiguration _config { get; set; }
    
            public ConfigurationHelper()
            {
                _config = new ConfigurationBuilder()
                .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })//ReloadOnChange = true 当appsettings.json被修改时重新加载
                .Build();
            }
    
            public T GetAppSettings<T>(string key) where T : class, new()
            {
                var appconfig = new ServiceCollection()
                    .AddOptions()
                    .Configure<T>(_config.GetSection(key))
                    .BuildServiceProvider()
                    .GetService<IOptions<T>>()
                    .Value;
                return appconfig;
            }
    
            public T Get<T>(string key)
            {
                IConfiguration config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", true, true).Build();
    
                return config.GetSection(key).Get<T>();
            }
    
            //public T GetAppsettings<T>(string key) where T : class, new()
            //{
            //    string keyDir = System.IO.Directory.GetCurrentDirectory();
    
            //    IConfiguration config = new ConfigurationBuilder()
            //        .SetBasePath(keyDir)
            //        .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })//ReloadOnChange = true 当appsettings.json被修改时重新加载
            //        .Build();
            //    var appconfig = new ServiceCollection()
            //        .AddOptions()
            //        .Configure<T>(config.GetSection(key))
            //        .BuildServiceProvider()
            //        .GetService<IOptions<T>>()
            //        .Value;
            //    return appconfig;
            //}
    
            /// <summary>
            /// 通过强类型绑定读取配置
            /// </summary>
            /// <typeparam name="Entity">要绑定的类型</typeparam>
            /// <param name="keyPath">配置文件路径</param>
            /// <returns>绑定的类</returns>
            public Entity GetSectionObject<Entity>(string keyPath = null) where Entity : new()
            {
                var entity = new Entity();
                if (string.IsNullOrEmpty(keyPath))
                {
                    //将appsettings.json全部配置绑定到模型
                    _config.Bind(entity);
                }
                else
                {
                    //将指定路径下的配置项绑定到模型
                    var section = _config.GetSection(keyPath);
                    section.Bind(entity);
                }
                return entity;
            }
        }

    方法二通过注入方式:

    using Microsoft.Extensions.Configuration;
    
        public class AppSettingOptions : IAppSetting
        {
            public AppSettingOptions()
            {
                var configBuilder = new ConfigurationBuilder();
                configBuilder.AddJsonFile("appsettings.json");
                var config = configBuilder.Build();
                var section = config.GetSection("AppSettings");
                section.Bind(this);
            }
    
            public int CacheDays { get; set; }
    
        }
    
        public interface ISingle
        {
        }
    
        public interface IScope
        {
        }
    
        public interface ITrans
        {
        }
    
        public interface IAppSetting : IScope { }

    方法二注入:

            readonly AppSettingOptions _config;
    
            public BoProcessIntelligenceShareHolder(IAppSetting config)
            {
                _config = (AppSettingOptions)config;
            }
  • 相关阅读:
    javac编译java文件报错:“3: 错误: 编码 GBK 的不可映射字符 (0xB2)”
    java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法
    MySQL下载、安装、配置(5.7.19版本)
    console报错:"-Djava.endorsed.dirs=D:apache-tomcat-9.0.7endorsed is not supported. Endorsed standards and standalone APIs in modular form will be supported via the concept of upgradeable modul"的原因及解决办法
    uDig配图与GeoServer添加Style
    ArcGIS统计栅格像元值并转换为矢量图层
    博客项目
    python(openpyxl)复制excel数据到另一个excel数据表
    python openpyxl自动化操作excel(xlsx)
    python自动化之 excel转word
  • 原文地址:https://www.cnblogs.com/hofmann/p/14982748.html
Copyright © 2011-2022 走看看