asp.net core 配置文件

 配置系统有3个核心对象

IConfigurationSource 代表配置数据最原始的来源

IConfigurationBuilder是IConfiguration对象的创建者

IConfiguration 读取配置信息的对象

 

一、在内存中读取配置文件

 

二、在持久化文件读取配置文件

appsettings.json 

{
  "ConnectionStrings": {
    "Default": "Server=.; Database=demoDb;"
  },
  "Subject": "english",
  "Student": [
    {
      "age": 15,
      "name": "tom"
    },
    {
      "age": 16,
      "name": "liu"
    }
  ],
  "SqlServer": {
    "Host": "192.168.1.0",
    "Port": 6215 
  }
}

 

控制台读取配置文件

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json

 

            var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            //读取连接数据库串
            Console.WriteLine(config.GetConnectionString("Default"));
            //读取节点值
            Console.WriteLine(config.GetSection("Subject").Value);
            //读取节点值的另一种方式
            Console.WriteLine(config["Subject"]);
            //读取嵌套内的值
            Console.WriteLine(config["SqlServer:Host"]);
            //读取Student第一个数组内的配置
            Console.WriteLine(config["Student:0:age"]);

            Console.ReadLine();

 读取成一个类

SqlServer sqlServer= config.GetSection("SqlServer").Get<SqlServer>();

 热加载配置文件

  var builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

 

ASP.Net Core中使用

不需要单独配置什么,直接依赖注入

    public class HomeController : Controller
    {
        private IConfigurationRoot ConfigRoot;

        public HomeController(IConfiguration configRoot)
        {
            ConfigRoot = (IConfigurationRoot)configRoot;
        }

        public IActionResult Index()
        {
            return Content(ConfigRoot["SqlServer:Host"]);
        }
    }

推荐读取方式:
当更改配置文件后
IOptions 一直读老的配置
IOptionsMonitor 立即读新的配置
IOptionsSnapshot 一个http请求内相同

        private IOptionsSnapshot<SqlServer> config;
        public HomeController(IOptionsSnapshot<SqlServer> configuration)
        {
            config = configuration;
        }


     ////////////////////////////////////////
        public void ConfigureServices(IServiceCollection services)
        {
            var builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            IConfigurationRoot configurationRoot = builder.Build();
            //读取节点
            services.AddOptions().Configure<SqlServer>(e => configurationRoot.GetSection("SqlServer").Bind(e));

            services.AddControllers();
        }

 

 

 

三、在数据库中读取配置文件

 

从命令行读取

 

从环境变量读取

 

扁平化配置

 

四、多环境

1.环境

为了确定运行时的环境,会从一下环境变量中读取信息

1.1 DOTNET_ENVIRONMENT

 

1.2  ASPNETCORE_ENVIRONMENT

  默认 ASP.NET Core Web 应用模板调用 ConfigureWebHostDefaults。 ASPNETCORE_ENVIRONMENT 值替代 DOTNET_ENVIRONMENT

  Development:launchSettings.json 文件将本地计算机上的 ASPNETCORE_ENVIRONMENT 设置为 Development

  Staging(预演)??

  Production:没有设置 DOTNET_ENVIRONMENT 和 ASPNETCORE_ENVIRONMENT 时的默认值

2.开发和 launchSettings.json

文件只能在本地开发环境中生效

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:45178",
      "sslPort": 0
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "webapitest": {
      "commandName": "Project",
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": "true",
      "applicationUrl": "http://localhost:5000"
    }
  }
}

 

commandName的值可以是三个:

2.1 IISExpress:启动 IIS Express

2.2 Project:启动 Kestre

2.3 IIS:不启动任何 Web 服务器。 IIS 预计可用. ???

 

也可以在项目属性中设置 launchSettings

 

 

可以自己添加其他配置,比如添加名字temp

 

 保存后再vs中可以看到多出的temp选项

 可以使用命令行调用自定义的temp选项

dotnet run --launch-profile "temp"

 

 

可以同时启用多个端口

 

 

 

3.生产环境

 

 

五、CreateDefaultBuilder配置文件读取顺序

1.代码里UseKestrel方法

 

 

2.优先读取命令行提供的配置

 

3.代码里UseUrls方法

 

 

4.其次是环境变量提供的配置

 

5.开发环境中的应用机密

 

6.json配置对象各环境的json

 

7.appsetting.json文件

 

8.主机配置

主机默认得配置

 

posted @ 2020-06-09 10:33  富坚老贼  阅读(288)  评论(0编辑  收藏  举报