.NETCore环境配置

使用默认配置,EnvironmentVariablesConfigurationProvider 会在读取 appsettings.json、appsettings.Environment.json 和机密管理器后从环境变量键值对加载配置 。 因此,从环境中读取的键值会替代从 appsettings.json、appsettings.Environment.json 和机密管理器中读取的值 。

 

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddEnvironmentVariables(prefix: "MyCustomPrefix_");
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

在上述代码中:

前缀会在读取配置键值对时被去除。

 

 

默认配置会加载前缀为 DOTNET_ 和 ASPNETCORE_ 的环境变量和命令行参数。 DOTNET_ 和 ASPNETCORE_ 前缀会由 ASP.NET Core 用于主机和应用配置,但不用于用户配置。 有关主机和应用配置的详细信息,请参阅 .NET 通用主机

 

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "http://localhost/CoreDemon",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:54936",
      "sslPort": 44323
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "CoreDemon": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_Name": "吴德群",
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}
launchSettings.json

这个文件是做环境配置的。本地调试经常用

 

 

 上面是IIS使用的默认配置

 

上面是各个环境的配置 

commandName    iis启动的是iis,iisExpress启动的是iisExpress,   Project启动的是本地项目(dotnet runn ,kestrel服务器)

 

使用 dotnet run 启动应用时,使用具有 "commandName": "Project" 的第一个配置文件。 commandName 的值指定要启动的 Web 服务器。 commandName 可为以下任一项:

  • IISExpress
  • IIS
  • Project(启动 Kestrel 的项目)

使用 dotnet run 启动应用时:

  • 如果可用,读取 launchSettings.json 。 launchSettings.json 中的 environmentVariables 设置会替代环境变量 。
  • 此时显示承载环境。

以下输出显示了使用 dotnet run 启动的应用:

Bash
PS C:\Websites\EnvironmentsSample> dotnet run
Using launch settings from C:\Websites\EnvironmentsSample\Properties\launchSettings.json...
Hosting environment: Staging
Content root path: C:\Websites\EnvironmentsSample
Now listening on: http://localhost:54340
Application started. Press Ctrl+C to shut down.

 launchSettings.json 不应存储机密 。 机密管理器工具可用于存储本地开发的机密。

 在没有 launchSettings.json 文件的 Development 环境中启动应用时,需要使用环境变量设置环境或者将命令行参数设为 dotnet run 命令 。

设置环境

通常,可以使用环境变量或平台设置来设置用于测试的特定环境。 如果未设置环境,默认值为 Production,这会禁用大多数调试功能。 设置环境的方法取决于操作系统。

构建主机时,应用读取的最后一个环境设置将决定应用的环境。 应用运行时无法更改应用的环境。

 在 Windows 和 macOS 上,环境变量和值不区分大小写。 默认情况下,Linux 环境变量和值要区分大小写 。

 

 

 

 

上面json配置文件对应这个里面的配置

 

posted @ 2020-06-17 00:02  西伯利亚的狼  阅读(437)  评论(0编辑  收藏  举报