当前位置: 移动技术网 > IT编程>开发语言>.net > (10)ASP.NET Core 中的环境配置

(10)ASP.NET Core 中的环境配置

2019年06月12日  | 移动技术网IT编程  | 我要评论

青春校园,忘记我是谁剧情介绍,冯巩的155句经典语

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 中使用多个环境

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网