ASP.NET Core 1.0 Configuration 配置管理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET Core 1.0 Configuration 配置管理相关的知识,希望对你有一定的参考价值。

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();
    }

以上是关于ASP.NET Core 1.0 Configuration 配置管理的主要内容,如果未能解决你的问题,请参考以下文章

NET Core 1.0

ASP.NET Core 1.0 基础与应用启动

.NET Core & ASP.NET Core 1.0

ASP.NET 5 改名 ASP.NET Core 1.0

ASP.NET Core 1.0 中使用Log日志

ASP.NET Core 1.0 中使用 Swagger 生成文档