1. 环境变量配置
ASP.NET Core 在应用程序启动时读取环境变量 (Properties\launchSettings.JSON)ASPNETCORE_ENVIRONMENT, 并将该值存储在 IHostingEnvironment.EnvironmentName 中. ASPNETCORE_ENVIRONMENT 可设置为任意值, 但框架只支持三个值: Development(开发),Staging (分阶段) 和 Production(生产). 如果未设置 ASPNETCORE_ENVIRONMENT, 则默认为 Production.
- public void Configure(IApplicationBuilder App, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- App.UseDeveloperExceptionPage();
- }
- if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
- {
- App.UseExceptionHandler("/Error");
- }
- }
Properties/launchSettings.JSON 里面的配置如下:
●当 ASPNETCORE_ENVIRONMENT 设置为 Development 时, 调用 UseDeveloperExceptionPage.
●当 ASPNETCORE_ENVIRONMENT 设置为 Staging,Production 时, 调用 UseExceptionHandler.
2. 开发环境配置
开发环境可以启用不应该在生产中公开的功能. 例如, 只在开发环境中启用了开发人员异常页. 本地计算机开发环境可以在项目的 Properties\launchSettings.JSON 文件中设置. 在 launchSettings.JSON 中设置的环境值替代在系统环境中设置的值. 以下 launchSettings.JSON 文件中显示的三个配置文件:
- {
- "iisSettings": {
- "windowsAuthentication": false,
- "anonymousAuthentication": true,
- "iisExpress": {
- "applicationUrl": "http://localhost:54339/",
- "sslPort": 0
- }
- },
- "profiles": {
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
- "environmentVariables": {
- "ASPNETCORE_My_Environment": "1",
- "ASPNETCORE_DETAILEDERRORS": "1",
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
- "EnvironmentsSample": {
- "commandName": "Project",
- "launchBrowser": true,
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Production"
- },
- "applicationUrl": "http://localhost:54340;http://localhost:54341"
- },
- "Kestrel Staging": {
- "commandName": "Project",
- "launchBrowser": true,
- "environmentVariables": {
- "ASPNETCORE_My_Environment": "1",
- "ASPNETCORE_DETAILEDERRORS": "1",
- "ASPNETCORE_ENVIRONMENT": "Staging"
- },
- "applicationUrl": "http://localhost:51997/"
- }
- }
- }
使用 dotnet run 启动应用时, 会使用具有 "commandName": "IISExpress" 的第一个配置文件. commandName 的值是指定要启动的 web 服务器. 而 launchSettings.JSON 中的 applicationUrl 属性也可指定服务器 URL 的列表. 在列表中的 URL 之间使用分号, 如上述环境配置中 EnvironmentsSample 里面的 applicationUrl 属性值配置. Visual Studio 项目属性 "调试" 选项卡中也提供了 GUI 来编辑 launchSettings.JSON 文件:
在 Web 服务器重新启动之前, 对项目配置文件所做的更改可能不会生效. 必须重新启动 Kestrel 才能检测到对环境配置所做的更改.
现在我们来验证开发环境中启用了开发人员异常页示例, 首先调试启动第一个配置文件(IISExpress):
3. 生产环境配置
Production 环境应配置为最大限度地提高安全性, 性能和应用可靠性. 不同于开发的一些通用设置包括:
●缓存.
●客户端资源被捆绑和缩小, 并可能从 CDN(网络分发)提供.
●已禁用诊断错误页.
●已启用友好错误页.
●已启用生产记录和监视. 例如, Application Insights.
现在我们来验证生产环境中启用了友好错误页示例, 首先调试启动第二个配置文件(EnvironmentsSample):
4. 基于环境配置的 Startup 类和方法
当 ASP.NET Core 应用程序启动时, 应用程序可以为不同的环境单独定义 Startup 类(例如, StartupDevelopment), 对应 Startup 类会在运行时进行选择环境配置. 优先考虑名称后缀与当前环境相匹配的 Startup 类. 如果找不到匹配的 Startup{EnvironmentName}, 就会使用原始的 Startup 类. 若要实现基于环境的 Startup 类, 请为使用中的每个环境创建 Startup{EnvironmentName} 类:
- public class StartupDevelopment
- {
- public void ConfigureServices(IServiceCollection services)
- {
- }
- public void Configure(IApplicationBuilder App, IHostingEnvironment env)
- {
- }
- }
- public class StartupProduction
- {
- public void ConfigureServices(IServiceCollection services)
- {
- }
- public void Configure(IApplicationBuilder App, IHostingEnvironment env)
- {
- }
- }
使用接受程序集名称的 UseStartup(IWebHostBuilder, String) 进行重载:
- public class Program
- {
- public static void Main(string[] args)
- {
- CreateWebHostBuilder(args).Build().Run();
- }
- public static IWebHostBuilder CreateWebHostBuilder(string[] args)
- {
- var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName;
- return WebHost.CreateDefaultBuilder(args)
- .UseStartup(assemblyName);
- }
- }
通过调试启动第二个配置文件 (EnvironmentsSample) 看看效果:
因为调试启动第二个配置文件 (EnvironmentsSample) 的生产 (Production) 环境, 所以 Startup 类会在运行选择时会针对当前环境配置找到对应 Startup 类并加载.
参考文献:
在 ASP.NET Core 中使用多个环境
来源: https://www.cnblogs.com/wzk153/p/11004125.html