public class Startup { public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; WebHostEnvironment = webHostEnvironment; } public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; } public void ConfigureServices(IServiceCollection services) { if (WebHostEnvironment.IsDevelopment()) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsTest()) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsStaging()) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsProduction()) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } } public void Configure(IApplicationBuilder app) { if (WebHostEnvironment.IsDevelopment()) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsTest()) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsStaging()) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } else if (WebHostEnvironment.IsProduction()) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } } } public static class AppHostEnvironmentEnvExtensions { public static bool IsTest(this IHostEnvironment hostEnvironment) { if (hostEnvironment == null) { throw new ArgumentNullException(nameof(hostEnvironment)); } return hostEnvironment.IsEnvironment(AppEnvironments.Test); } } public static class AppEnvironments { public static readonly string Test = nameof(Test); }
2.Startup 方法约定上面的方式把不同环境的代码放在了同一个方法中,看起来比较混乱也不容易区分。因此我们希望ConfigureServices和Configure能够根据不同的环境进行代码拆分。
我们可以通过方法命名约定来解决,约定Configure{EnvironmentName}Services和Configure{EnvironmentName}Services来装载不同环境的代码。如果当前环境没有对应的方法,则使用原来的ConfigureServices和Configure方法。
我就只拿 Development 和 Production 举例了
public class Startup { // 我这里注入 IWebHostEnvironment,仅仅是为了打印出来当前环境信息 public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; WebHostEnvironment = webHostEnvironment; } public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; } #region ConfigureServices private void StartupConfigureServices(IServiceCollection services) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } public void ConfigureDevelopmentServices(IServiceCollection services) { StartupConfigureServices(services); } public void ConfigureProductionServices(IServiceCollection services) { StartupConfigureServices(services); } public void ConfigureServices(IServiceCollection services) { StartupConfigureServices(services); } #endregion #region Configure private void StartupConfigure(IApplicationBuilder app) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } public void ConfigureDevelopment(IApplicationBuilder app) { StartupConfigure(app); } public void ConfigureProduction(IApplicationBuilder app) { StartupConfigure(app); } public void Configure(IApplicationBuilder app) { StartupConfigure(app); } #endregion }
3.Startup 类约定该方式适用于多环境下,代码差异较大的情况。
程序启动时,会优先寻找当前环境命名符合Startup{EnvironmentName}的 Startup 类,如果找不到,则使用名称为Startup的类
首先,CreateHostBuilder方法需要做一处修改
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { //webBuilder.UseStartup<Startup>(); webBuilder.UseStartup(typeof(Startup).GetTypeInfo().Assembly.FullName); });
接下来,就是为各个环境定义 Startup 类了(我就只拿 Development 和 Production 举例了)
public class StartupDevelopment { // 我这里注入 IWebHostEnvironment,仅仅是为了打印出来当前环境信息 public StartupDevelopment(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; WebHostEnvironment = webHostEnvironment; } public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; } public void ConfigureServices(IServiceCollection services) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } public void Configure(IApplicationBuilder app) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } } public class StartupProduction { public StartupProduction(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; WebHostEnvironment = webHostEnvironment; } public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; } public void ConfigureServices(IServiceCollection services) { Console.WriteLine($"{nameof(ConfigureServices)}: {WebHostEnvironment.EnvironmentName}"); } public void Configure(IApplicationBuilder app) { Console.WriteLine($"{nameof(Configure)}: {WebHostEnvironment.EnvironmentName}"); } }