ASP.NET Core 2.2 基础知识(五) 环境

一.环境变量

系统启动时,会读取环境变量 ASPNETCORE_ENVIRONMENT ,并将该变量的值存储在 IHostingEnvironment.EnvironmentName 字段中.如:

新建一个 WebAPI 项目,修改 Configure 方法:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ......
            {
                app.Run(async context =>
                {
                    context.Response.ContentType = "text/plain;charset=utf-8";//没有这句话,中文会出现乱码.
                    await context.Response.WriteAsync($"当前环境 : {env.EnvironmentName}");
                });
            }
            ......
        }

ASPNETCORE_ENVIRONMENT 可以设置为任意值,但是 ASP.NET Core 框架只支持 3 个值:

Development 

Staging

Production

如果没有设置该变量的值,则默认 Production

那么问题来了,在哪里设置呢?

方法一 : launchSettings.json

打开上例创建的 WebAPI 项目的 launchSettings.json 文件:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:53476",
      "sslPort": 44307
    }
  },
  "profiles": {
    //部署到IIS时,会读取该节点的设置.
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        //"ASPNETCORE_ENVIRONMENT": "Development"
        "ASPNETCORE_ENVIRONMENT": "Production" //我们修改为 "Production" 看效果
      }
    },
    //在控制台启动时,会读取该节点的设置
    "EnvironmentDemo1": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging" //我们修改为 "Staging" 看效果
      }
    }
  }
}

IIS下运行效果:

控制台运行效果:

 二.基于环境的 Startup 类

当 ASP.NET Core 应用启动时,会在 Program 类中通过 Startup 类启动应用.

ASP.NET Core 提供了基于不同环境选择不同的 Startup 类的方法.但是类名有个约定,只能这样命名:

Startup{EnvironmentName}

EnvironmentName 为当前环境.

示例:

    /// <summary>
    /// 开发环境启动类
    /// </summary>
    public class StartupDevelopment
    {
        public void ConfigureServices(IServiceCollection services){}

        public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
    }

    /// <summary>
    /// 模拟环境启动类
    /// </summary>
    public class StartupStaging
    {
        public void ConfigureServices(IServiceCollection services){}

        public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
    }

    /// <summary>
    /// 生产环境启动类
    /// </summary>
    public class StartupProduction
    {
        public void ConfigureServices(IServiceCollection services){}

        public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
    }

要让系统根据当前环境选择上述3个启动类中的一个,还需要修改调用方法.

默认的调用方法是这样的:

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
    }

需要修改成:

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName;
            return WebHost.CreateDefaultBuilder(args).UseStartup(assemblyName);
        }

 二.基于环境的 ConfigureServices 方法和 Configure 方法

上代码一目了然:

        public void ConfigureStaging(IApplicationBuilder app, IHostingEnvironment env)
        {

        }

        public void ConfigureStagingServices(IServiceCollection services)
        {

        }
原文地址:https://www.cnblogs.com/refuge/p/10219223.html