(10)ASP.NET Core 中的環境配置
1.環境變數配置
ASP.NET Core在應用程式啟動時讀取環境變數(Properties\launchSettings.json)ASPNETCORE_ENVIRONMENT,並將該值儲存在IHostingEnvironment.EnvironmentName中。ASPNETCORE_ENVIRONMENT可設定為任意值,但框架只支援三個值:Development(開發)、Staging (分階段)和 Production(生產)。如果未設定ASPNETCORE_ENVIRONMENT,則預設為 Production。
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2")) { app.UseExceptionHandler("/Error"); } }
Properties/launchSettings.json裡面的配置如下:
●當ASPNETCORE_ENVIRONMENT設定為Development時,呼叫UseDeveloperExceptionPage。
●當ASPNETCORE_ENVIRONMENT設定為Staging、Production時,呼叫UseExceptionHandler。
2.開發環境配置
開發環境可以啟用不應該在生產中公開的功能。例如,只在開發環境中啟用了開發人員異常頁。本地計算機開發環境可以在專案的Properties\launchSettings.json檔案中設定。在 launchSettings.json中設定的環境值替代在系統環境中設定的值。以下 launchSettings.json 檔案中顯示的三個配置檔案:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:54339/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_My_Environment": "1", "ASPNETCORE_DETAILEDERRORS": "1", "ASPNETCORE_ENVIRONMENT": "Development" } }, "EnvironmentsSample": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Production" }, "applicationUrl": "http://localhost:54340;http://localhost:54341" }, "Kestrel Staging": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_My_Environment": "1", "ASPNETCORE_DETAILEDERRORS": "1", "ASPNETCORE_ENVIRONMENT": "Staging" }, "applicationUrl": "http://localhost:51997/" } } }
使用dotnet run啟動應用時,會使用具有"commandName": "IISExpress"的第一個配置檔案。commandName的值是指定要啟動的Web伺服器。而launchSettings.json中的applicationUrl屬性也可指定伺服器URL的列表。 在列表中的URL之間使用分號,如上述環境配置中EnvironmentsSample裡面的applicationUrl屬性值配置。Visual Studio專案屬性“除錯”選項卡中也提供了GUI來編輯launchSettings.json檔案:
在Web伺服器重新啟動之前,對專案配置檔案所做的更改可能不會生效。必須重新啟動 Kestrel才能檢測到對環境配置所做的更改。
現在我們來驗證開發環境中啟用了開發人員異常頁示例,首先除錯啟動第一個配置檔案(IISExpress):
3.生產環境配置
Production環境應配置為最大限度地提高安全性、效能和應用可靠性。不同於開發的一些通用設定包括:
●快取。
●客戶端資源被捆綁和縮小,並可能從CDN(網路分發)提供。
●已禁用診斷錯誤頁。
●已啟用友好錯誤頁。
●已啟用生產記錄和監視。例如,Application Insights。
現在我們來驗證生產環境中啟用了友好錯誤頁示例,首先除錯啟動第二個配置檔案(EnvironmentsSample):
4.基於環境配置的Startup類和方法
當ASP.NET Core應用程式啟動時,應用程式可以為不同的環境單獨定義Startup類(例如,StartupDevelopment),對應Startup類會在執行時進行選擇環境配置。優先考慮名稱字尾與當前環境相匹配的Startup類。如果找不到匹配的Startup{EnvironmentName},就會使用原始的Startup類。若要實現基於環境的Startup類,請為使用中的每個環境建立Startup{EnvironmentName} 類:
public class StartupDevelopment { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { } } public class StartupProduction { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { } }
使用接受程式集名稱的UseStartup(IWebHostBuilder, String) 進行過載:
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) { var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName; return WebHost.CreateDefaultBuilder(args) .UseStartup(assemblyName); } }
通過除錯啟動第二個配置檔案(EnvironmentsSample)看看效果:
因為除錯啟動第二個配置檔案(EnvironmentsSample)的生產(Production)環境,所以Startup類會在執行選擇時會針對當前環境配置找到對應Startup類並載入。
參考文獻:
在 ASP.NET Core 中使用多個環境