Portal

Time:2021-9-11

The configuration method of aspnet core multi environment is based on the startup class.

  1. WebBuilder. Usestartup() / / explicitly register and target startup.cs
  2. webBuilder.UseStartup(Assembly.GetExecutingAssembly().FullName); // Multi environment writing method: select the class based on the agreed name according to the environment variables

There are two other methods, which are not described in detail here. It is obvious that configuring multiple environments does not use explicit registration. Therefore, mode II is adopted for the following tests

Try 1: priorities of different environments

Note: many convention based configurations in. Net core are generally reflected in the name. for example  Startup{EnvironmentName}   It is a startup item that specifies the environment

PortalPortal

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("appsettings.Production.json").Build();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}

default

PortalPortal

public class StartupProduction
{
    public void ConfigureServices(IServiceCollection services)
    {
        IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("appsettings.Production.json").Build();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}

Production environment

Conclusion:Obviously, environmental variables take precedence

Try 2: priorities under different namespaces

PortalPortal

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("appsettings.Production.json").Build();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}

No namespace

PortalPortal

namespace Other
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("appsettings.Production.json").Build();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        }
    }
}

Any namespace

PortalPortal

namespace AspnetCore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("appsettings.Production.json").Build();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        }
    }
}

Current namespace

Conclusion:It is reasonable to have no namespace priority, but it is surprising that any namespace is higher than the current priority set

follow-up

First, the multi environment configuration mode can also be selected based on the configuration file《Portal》, and selection based on configuration method

Second: the other two methods are

  1. WebBuilder. Configure (APP = > {}) / / register a delegatestartup object
  2. WebBuilder. Usesetting (“startupassembly”, assembly. Getentryassembly(). Fullname) / / direct specifying method