asp.net-core Configuring multiple Environments Configuring multiple environments

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

This example shows how to configure multiple environments with different Dependency Injection configuration and separate middlewares in one Startup class.

Alongside of public void Configure(IApplicationBuilder app) and public void ConfigureServices(IServiceCollection services) methods one can use Configure{EnvironmentName} and Configure{EnvironmentName}Services to have environment dependent configuration.

Using this pattern avoids putting to much if/else logic withing one single method/Startup class and keep it clean and separated.

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) { }
}

When a Configure{Environmentname} or Configure{Environmentname}Services is not found, it will fall back to Configure or ConfigureServices respectively.

The same semantics also apply to the Startup class. StartupProduction will be used when the ASPNETCORE_ENVIRONMENT variable is set to Production and will fall back to Startup when it's Stagging or Development

A complete example:

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();
    }
}


Got any asp.net-core Question?