新建项目

新建ASP.NET Core Web 应用程序

image.png

Configuration加载顺序

  1. namespace Microsoft.Extensions.Hosting
  2. {
  3. //
  4. // 摘要:
  5. // Provides convenience methods for creating instances of Microsoft.Extensions.Hosting.IHostBuilder
  6. // with pre-configured defaults.
  7. public static class Host
  8. {
  9. //
  10. // 摘要:
  11. // Initializes a new instance of the Microsoft.Extensions.Hosting.HostBuilder class
  12. // with pre-configured defaults.
  13. //
  14. // 返回结果:
  15. // The initialized Microsoft.Extensions.Hosting.IHostBuilder.
  16. //
  17. // 言论:
  18. // The following defaults are applied to the returned Microsoft.Extensions.Hosting.HostBuilder:
  19. // • set the Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath to the
  20. // result of System.IO.Directory.GetCurrentDirectory
  21. // • load host Microsoft.Extensions.Configuration.IConfiguration from "DOTNET_"
  22. // prefixed environment variables
  23. // • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
  24. // and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
  25. // • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
  26. // when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName is 'Development'
  27. // using the entry assembly
  28. // • load app Microsoft.Extensions.Configuration.IConfiguration from environment
  29. // variables
  30. // • configure the Microsoft.Extensions.Logging.ILoggerFactory to log to the console,
  31. // debug, and event source output
  32. // • enables scope validation on the dependency injection container when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName
  33. // is 'Development'
  34. public static IHostBuilder CreateDefaultBuilder();
  35. //
  36. // 摘要:
  37. // Initializes a new instance of the Microsoft.Extensions.Hosting.HostBuilder class
  38. // with pre-configured defaults.
  39. //
  40. // 参数:
  41. // args:
  42. // The command line args.
  43. //
  44. // 返回结果:
  45. // The initialized Microsoft.Extensions.Hosting.IHostBuilder.
  46. //
  47. // 言论:
  48. // The following defaults are applied to the returned Microsoft.Extensions.Hosting.HostBuilder:
  49. // • set the Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath to the
  50. // result of System.IO.Directory.GetCurrentDirectory
  51. // • load host Microsoft.Extensions.Configuration.IConfiguration from "DOTNET_"
  52. // prefixed environment variables
  53. // • load host Microsoft.Extensions.Configuration.IConfiguration from supplied command
  54. // line args
  55. // • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
  56. // and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
  57. // • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
  58. // when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName is 'Development'
  59. // using the entry assembly
  60. // • load app Microsoft.Extensions.Configuration.IConfiguration from environment
  61. // variables
  62. // • load app Microsoft.Extensions.Configuration.IConfiguration from supplied command
  63. // line args
  64. // • configure the Microsoft.Extensions.Logging.ILoggerFactory to log to the console,
  65. // debug, and event source output
  66. // • enables scope validation on the dependency injection container when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName
  67. // is 'Development'
  68. public static IHostBuilder CreateDefaultBuilder(string[] args);
  69. }
  70. }

注意这段注释

  1. // • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
  2. // and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
  3. // • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
  1. ```csharp
  2. public static class Program
  3. {
  4. public static void Main(string[] args)
  5. {
  6. CreateHostBuilder(args).Build().Run();
  7. }
  8. public static IHostBuilder CreateHostBuilder(string[] args) =>
  9. Host.CreateDefaultBuilder(args)
  10. .ConfigureWebHostDefaults(webBuilder =>
  11. {
  12. webBuilder.UseStartup<Startup>();
  13. });
  14. }

就是说Host.CreateDefaultBuilder()加载Configuration的顺序是

  1. appsettings.json
  2. appsettings.{envName}.json
  3. usesecrets(secrets.json)

同一个节点信息如果在三个.json文件中都有定义,会依赖加载顺序,最后的会覆盖之前的

多环境配置

image.png

这里的设计思路是将全局通用配置放在`appsettings.json中,不同环境根据需求配置自己的节点,也可以使用相同名称来覆盖appsettings.json

appsettings.json

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Information"
  5. }
  6. },
  7. "AllowedHosts": "*"
  8. }

appsettings.Development.json

  1. {
  2. "ConnectString": "Server=.;Database=WebApp01.Db.Dev;uid=sa;pwd=wpl19950815;"
  3. }

appsettings.Sit.json

  1. {
  2. "ConnectString": "Server=.;Database=WebApp01.Db.Sit;uid=sa;pwd=wpl19950815;"
  3. }

调试不同环境

image.png

通过设置 environmentVariables:ASPNETCORE_ENVIRONMENT环境名称来切换使用不同环境的配置文件,这里环境变量名称需要和appsetting.{envName}.json{envName}保持一致