[C#,windows]設定windows環境變數,與自動切換SectionGroup 取得需要的appSetting資料
寫好的程式,有時候可能部屬到不同的環境上,而有時候不同環境還有可能搭配不同的參數,此時寫在web.config的參數要如何切換不同環境取得不同參數值呢?
這邊提供一個思維,首先我們在各個環境上都建立一個環境變數,讓我們程式可以辨認目前是在哪一個環境上(若此環境變數未建立,預設是本機環境),並在web.config上建立「sectionGroup」,給予不同的「appSettings」值,如此一來便可以達到需求效果。
實作:
1.設定系統環境參數
(1)以系統管理員開啟PowerShell
<!--本機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "Dev", "Machine")
<!--客戶正式機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "pr", "Machine")
<!--客戶測試機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "qa", "Machine")
<!--公司測試機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "ta", "Machine")
[environment]::SetEnvironmentvariable("Test_STAGING", "Dev", "Machine")
<!--客戶正式機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "pr", "Machine")
<!--客戶測試機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "qa", "Machine")
<!--公司測試機-->
[environment]::SetEnvironmentvariable("Test_STAGING", "ta", "Machine")
ps:「Test_STAGING」是環境變數名稱,可以隨意取,Dev,pr,qa,ta通常代表本機,客戶正式機,客戶測試機,公司測試機可以依環境選擇設定,這邊範例選擇設定的是本基環境。
*檢查是否有設定成功
[environment]::GetEnvironmentVariable('Test_STAGING', "Machine")
*方法二 在 進階系統設定 設定環境變數
2.建立 終端解析 Helper
/// <summary>
/// Endpoint Helper
/// </summary>
public static class EndpointHelper
{
public static readonly NameValueCollection AppSettings = GetConfigurationUsingAppSettings();
/// <summary>
/// 取得當前環境 AppSettings
/// </summary>
/// <returns></returns>
private static NameValueCollection GetConfigurationUsingAppSettings()
{
var configName = GetMachineAppSettings();
var postSetting = ConfigurationManager.GetSection(configName) as NameValueCollection;
return postSetting;
}
/// <summary>
/// 取得目前程式運行設備AppSettings
/// </summary>
/// <returns></returns>
private static string GetMachineAppSettings()
{
var value = GetEnvironmentVariable("Test_STAGING");
var machineName = value == null ? string.Empty : value.Trim().ToLower();
var DefaultmachineName = nameof(EndpointType.dev);
var group = "/appSettings";
string[] scopes = { nameof(EndpointType.dev), nameof(EndpointType.ta), nameof(EndpointType.qa), nameof(EndpointType.pr) };
var scope = scopes.FirstOrDefault(x => x == value);
machineName = string.IsNullOrWhiteSpace(scope) ? DefaultmachineName : scope;
return machineName + group;
}
/// <summary>
/// 取得環境變數
/// </summary>
/// <param name="name"></param>
/// <param name="target"></param>
/// <returns></returns>
public static string GetEnvironmentVariable(string name, EnvironmentVariableTarget target = default)
{
var value = default(string);
for (EnvironmentVariableTarget t = target; t <= EnvironmentVariableTarget.Machine; t++)
{
if (!string.IsNullOrWhiteSpace(value = Environment.GetEnvironmentVariable(name, t)))
{
break;
}
}
return value;
}
}
/// <summary>
/// 終端類型
/// </summary>
public enum EndpointType
{
/// <summary>
/// 本機
/// </summary>
[Display(Name = "本機", Description = "本機")]
dev,
/// <summary>
/// 公司測試機
///</summary>
[Display(Name = "公司測試機", Description = "公司測試機")]
ta,
/// <summary>
/// 客戶測試機
/// </summary>
[Display(Name = "客戶測試機", Description = "客戶測試機")]
qa,
/// <summary>
/// 客戶正式機
/// </summary>
[Display(Name = "客戶正式機", Description = "客戶正式機")]
pr,
}
3.在web.config 寫入sectionGroup 以及各環境appSettings參數
<!--sectionGroup--> <configSections> <sectionGroup name="dev"> <section name="appSettings" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> <sectionGroup name="ta"> <section name="appSettings" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> <sectionGroup name="qa"> <section name="appSettings" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> <sectionGroup name="pr"> <section name="appSettings" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> </configSections> <!--本機--> <dev> <appSettings> <add key="NowEnvironment" value="devTest" /> </appSettings> </dev> <!--公司測試機--> <ta> <appSettings> <add key="NowEnvironment" value="taTest" /> </appSettings> </ta> <!--客戶測試機--> <qa> <appSettings> <add key="NowEnvironment" value="qaTes" /> </appSettings> </qa> <!--客戶正式機--> <pr> <appSettings> <add key="NowEnvironment" value="prTest" /> </appSettings> </pr>
4.結果:結果取得本機所需「NowEnvironment」參數值
public class HomeController : Controller
{
public ActionResult Index()
{
var NowEnvironment = EndpointHelper.AppSettings["NowEnvironment"];
return View();
}
}
留言
張貼留言