zoukankan      html  css  js  c++  java
  • (10)ASP.NET Core2.2 中的环境(Environments:dev, stage, prod)

    1.环境变量配置

    ASP.NET Core在应用程序启动时读取环境变量(PropertieslaunchSettings.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.开发环境配置

    开发环境可以启用不应该在生产中公开的功能。例如,只在开发环境中启用了开发人员异常页。本地计算机开发环境可以在项目的PropertieslaunchSettings.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 中使用多个环境

  • 相关阅读:
    pl2303 驱动
    tomcat 启动脚本
    Linux下Shell命令加减乘除计算
    定时删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件
    mysql 拼接字符
    jquery iframe父子框架中的元素访问方法
    在线工具
    js对数组对象的操作以及方法的使用
    HTML 设置字体
    10月1号 备忘录
  • 原文地址:https://www.cnblogs.com/wzk153/p/11004125.html
Copyright © 2011-2022 走看看