配置多个环境

此示例显示如何在一个 Startup 类中配置具有不同依赖注入配置和单独中间件的多个环境。

除了 public void Configure(IApplicationBuilder app)public void ConfigureServices(IServiceCollection services) 方法之外,可以使用 Configure{EnvironmentName}Configure{EnvironmentName}Services 来进行依赖于环境的配置。

使用这种模式可以避免使用单个方法/ Startup 类来实现更多的 if/else 逻辑并保持清洁和分离。

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }
    public void ConfigureStaggingServices(IServiceCollection services) { }
    public void ConfigureProductionServices(IServiceCollection services) { }
    
    public void Configure(IApplicationBuilder app) { }
    public void ConfigureStagging(IApplicationBuilder app) { }
    public void ConfigureProduction(IApplicationBuilder app) { }
}

当找不到 Configure{Environmentname}Configure{Environmentname}Services 时,它将分别回落到 ConfigureConfigureServices

相同的语义也适用于 Startup 类。当 ASPNETCORE_ENVIRONMENT 变量设置为 Production 时将使用 StartupProduction,当它是 StaggingDevelopment 时将回落到 Startup

一个完整的例子:

public class Startup
{
    public Startup(IHostingEnvironment hostEnv)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostEnv.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true);

        if (hostEnv.IsDevelopment())
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container
    public static void RegisterCommonServices(IServiceCollection services) 
    {
        services.AddScoped<ICommonService, CommonService>();
        services.AddScoped<ICommonRepository, CommonRepository>();
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        RegisterCommonServices(services);
        
        services.AddOptions();
        services.AddMvc();
    }

    public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();

        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();
        app.UseStaticFiles();
        app.UseMvc();
    }

    // No console Logger and debugging tools in this configuration
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddDebug();

        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();
        app.UseStaticFiles();
        app.UseMvc();
    }
}