• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET Core 1.0 Configuration 配置管理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

documentation: https://docs.asp.net/en/latest/fundamentals/configuration.html
github: https://github.com/aspnet/Configuration/

  • 配置的接口定义与基础实现
    • Microsoft.Extensions.Configuration  配置文件的基础实现
    • Microsoft.Extensions.Configuration.Abstractions  配置文件的基础实现的接口定义
    • Microsoft.Extensions.Configuration.Binder  特殊配置文件实现
  • 配置的扩展
    • Microsoft.Extensions.Configuration.CommandLine  命令行扩展
    • Microsoft.Extensions.Configuration.EnvironmentVariables  环境变量扩展
    • Microsoft.Extensions.Configuration.FileExtensions  文本类型扩展
    • Microsoft.Extensions.Configuration.FileProviderExtensions 用来检测配置文本是否变动
    • Microsoft.Extensions.Configuration.Ini  Ini文件类型扩展
    • Microsoft.Extensions.Configuration.Json Json文件类型扩展
    • Microsoft.Extensions.Configuration.Xml   Xml文件类型扩展

ASP.NET Core 1.0 中抛弃了原先的web.config文件机制,引用了现有的appsettings.json文件机制,配置的文件的类型可以是JSON,XML,INI等,如在Startup类中:

    /// <summary> 
    /// 配置信息 
     /// </summary> 
    public IConfigurationRoot Configuration { get; set; }

    /// <summary> 
    /// 程序入口点 
    /// </summary> 
    /// <param name="env"></param> 
    public Startup(IHostingEnvironment env) 
    { 
        var builder = new ConfigurationBuilder() 
                     .AddJsonFile("appsettings.json") 
                     .AddEnvironmentVariables(); 
        Configuration = builder.Build(); 
    }

新的配置机制基于Microsoft.Extensions.Configuration命名空间,IConfiguration接口中定义配置信息的实例接口

    /// <summary>
    /// Represents a set of key/value application configuration properties.
    /// </summary>
    public interface IConfiguration
    {
        /// <summary>
        /// Gets or sets a configuration value.
        /// </summary>
        /// <param name="key">The configuration key.</param>
        /// <returns>The configuration value.</returns>
        string this[string key] { get; set; }

        /// <summary>
        /// Gets a configuration sub-section with the specified key.
        /// </summary>
        /// <param name="key">The key of the configuration section.</param>
        /// <returns>The <see cref="IConfigurationSection"/>.</returns>
        /// <remarks>
        ///     This method will never return <c>null</c>. If no matching sub-section is found with the specified key,
        ///     an empty <see cref="IConfigurationSection"/> will be returned.
        /// </remarks>
        IConfigurationSection GetSection(string key);

        /// <summary>
        /// Gets the immediate descendant configuration sub-sections.
        /// </summary>
        /// <returns>The configuration sub-sections.</returns>
        IEnumerable<IConfigurationSection> GetChildren();

        IChangeToken GetReloadToken();
    }

IConfigurationRoot接口继承IConfiguration接口,定义了Reload方法; IConfigurationProvider 是定义所有实现的基础接口约定;IConfigurationBuilder接口是基础实现的构造器,ConfigurationBuilder类定义了基础具体实现。

扩展方法

支持多文件类型配置,是在IConfigurationBuilder构造器接口上进行扩展方法

       var configurationBuilder = new ConfigurationBuilder();
       configurationBuilder.AddIniFile(_iniConfigFilePath);
       configurationBuilder.AddJsonFile(_jsonConfigFilePath);
       configurationBuilder.AddXmlFile(_xmlConfigFilePath);
       configurationBuilder.AddInMemoryCollection(_memConfigContent);
       var config = configurationBuilder.Build();

如AddJsonFile扩展方法

        /// <summary>
        /// Adds the JSON configuration provider at <paramref name="path"/> to <paramref name="configurationBuilder"/>.
        /// </summary>
        /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <param name="path">Absolute path or path relative to <see cref="IConfigurationBuilder.BasePath"/> of
        /// <paramref name="configurationBuilder"/>.</param>
        /// <param name="optional">Determines if loading the configuration provider is optional.</param>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        /// <exception cref="ArgumentException">If <paramref name="path"/> is null or empty.</exception>
        /// <exception cref="FileNotFoundException">If <paramref name="optional"/> is <c>false</c> and the file cannot
        /// be resolved.</exception>
        public static IConfigurationBuilder AddJsonFile(
            this IConfigurationBuilder configurationBuilder,
            string path,
            bool optional)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path));
            }

            var fullPath = Path.Combine(configurationBuilder.GetBasePath(), path);

            if (!optional && !File.Exists(fullPath))
            {
                throw new FileNotFoundException(Resources.FormatError_FileNotFound(fullPath), fullPath);
            }

            configurationBuilder.Add(new JsonConfigurationProvider(fullPath, optional: optional));

            return configurationBuilder;
        }

Application Secrets
https://github.com/aspnet/UserSecrets
Configuration
https://docs.asp.net/en/latest/fundamentals/configuration.html

http://developer.telerik.com/featured/new-configuration-model-asp-net-core/

http://jameschambers.com/2016/01/Strongly-Typed-Configuration-in-ASP-NET-Core-MVC/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap