When writing an ASP.NET Core application, you may get to a point where your Startup.cs class is getting far too big as you're doing lots of different things depending on your environment (Production, Development, Staging, AutoTests e.t.c).

If this is the case then you can have multiple Startup.cs classes depending on the environment, for example you could have a StartupAutoTests.cs file that bootstraps your app when the enviornment variable ASPNETCORE_ENVIRONMENT is set to AutoTests. To do this, you first have to create the new StartupAutoTest.cs file:

// Startup class to use in the Development environment
public class StartupAutoTests
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Maybe use an in memory database context for auto tests
        ...
    }

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

// Fallback Startup class
// Selected if the environment doesn't match a Startup{EnvironmentName} class
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ...
    }

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

You then have to change the CreateWebHostBuilder() method in your Program.cs file to be:

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

    return WebHost.CreateDefaultBuilder(args).UseStartup(assemblyName);
}

Instead of the templated method, which always looks for the default Startup class:

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

Configure and ConfigureServices Startup Methods

You can, if needed have ASP.NET Core call different Configure() and ConfigureServices() methods in one Startup class if there isn't the need for a completley new Startup class. The convention for this is Configure{EnvironmentName} and Configure{EnvironmentName}Services.

So in our previous example, instead of having a StartupAutoTest.cs class, we could have it all in the default Startup.cs class like so:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        //...
    }

    public void ConfigureAutoTestsServices(IServiceCollection services)
    {
        //...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseMvc();
    }

    public void ConfigureAutoTests(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (!env.IsAutoTests())
        {
            throw new Exception("Not Auto Tests.");
        }
        
        app.UseMvc();
    }
}

For more info and how to set Environment variables on Windows, Mac and Linux; view the full Microsoft documentation here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1