架构最佳实践 - 不同环境的配置






2.63/5 (5投票s)
大多数开发的系统会有不同的环境,例如,开发 (Development)、质量保证 (Quality Assurance) 或测试 (Testing) 和上线 (Live) 或生产 (Production)。本文介绍了如何在部署时整合所有三种环境的配置,而无需更改它们。
引言
大多数开发的系统会有不同的环境;例如,开发 (Development)、质量保证 (Quality Assurance) 或测试 (Testing) 和上线 (Live) 或生产 (Production)。本文介绍了如何在部署时整合所有三种环境的配置,而无需更改它们。
背景
我们中的许多人使用配置文件(如 web.config 或 app.config)中的连接字符串设置来开发应用程序。然后,这需要在上述不同环境中部署。为了让应用程序在特定环境中正常工作,我们必须更改此设置。这个过程必须为每一次发布重复进行。随着配置在每次迭代中添加越来越多的设置,保持配置文件的恒定成为一种奢望。如果这需要在许多此类设置上完成,我们确实会遇到问题。
本文解决了这个问题,主要面向开发框架的架构师或技术主管。
使用代码
这可以很容易地用于实现 N 层架构的系统的业务逻辑层。AppConfiguration
类(附带源代码)用于获取配置值,应该成为框架核心模块的一部分。所有访问配置的代码都应该使用这个类。对于其他系统,需要一个变通方法。请做好做出牺牲的准备,因为大多数 VS2005 代码生成器不考虑这种情况,但好处远远大于这些牺牲。
这适用于两种类型的配置文件:Web.config 或 App.config。为了清晰起见,对配置文件的任何引用都意味着其中之一。
要实现这一点,我们需要在配置文件中的 <system.diagnostics>
节点下添加一个 <switches>
节点。
<configuration>
<system.diagnostics>
<switches>
<!--
This is a trace switch. The follow are its values
0 - Off
1 - TraceError - Error
2 - TraceWarning - Error and Warning
3 - TraceInfo - Error, Warning and Info
4 - TraceVerbose - Everything
All the information which are lower than this setting will be activated.
eg.
if you set the value 3. The following will be activated
Error, Warning and Info
-->
<add name="Debug" value="0"/>
<!--
These are boolean switches.
0 - Disabled
1 - Enabled
Only one among the three should have a value of 1 [preferred]
rest should be 0.
-->
<add name="DevMode" value="1"/>
<add name="QAMode" value="0"/>
<add name="LiveMode" value="0"/>
</switches>
</system.diagnostics>
</configuration>
这里添加了四个性质相同的开关。它们被细分为两个部分:Trace Switch 和 Boolean Switch。“DevMode
”、“QAMode
”和“LiveMode
”开关分别指示不同的环境,并且如注释所示,在这三者之间只能有一个环境处于活动状态。与代码声明结合可以理解到转化为特定开关类型的区别。
这是激活和转换这些开关的代码声明。请注意:开关的名称应与声明匹配。
using System.Diagnostics;
//under class declaration
/// <summary>
/// <para>Used to write debug tracing information for the entire application.</para>
/// <para>This is enabled by having a switch declared inside web.config
/// system.diagnostics - switches node</para>
/// </summary>
public static TraceSwitch DebugSwitch = new TraceSwitch("Debug", "Debug mode");
/// <summary>
/// <para>Used to identify settings specific
/// to Development Phase for the entire application.</para>
/// <para>This is enabled by having a switch declared inside web.config
/// system.diagnostics - switches node</para>
/// </summary>
public static BooleanSwitch DevModeSwitch = new BooleanSwitch("DevMode",
"Identifies Development Mode");
/// <summary>
/// <para>Used to identify settings specific to Quality Assurance
/// or Testing Phase for the entire application.</para>
/// <para>This is enabled by having a switch declared inside web.config
/// system.diagnostics - switches node</para>
/// </summary>
public static BooleanSwitch QAModeSwitch = new BooleanSwitch("QAMode",
"Identifies Quality Assurance Mode");
/// <summary>
/// <para>Used to identify settings specific to Live Phase for the entire application.</para>
/// <para>This is enabled by having a switch declared inside web.config
/// system.diagnostics - switches node</para>
/// </summary>
public static BooleanSwitch LiveModeSwitch = new BooleanSwitch("LiveMode",
"Identifies Live Mode");
完成初始声明后,需要进行设置条目。只能使用 <appSettings>
节点。不是只有一个条目,而是为每个环境制作了特定的设置。这里为每个设置制作了三个条目:每个环境一个(Dev、QA 和 Live)。对于这些环境,设置分别带有后缀“_Dev”、“_QA”和无后缀。
<configuration>
<appSettings>
<!-- Start of Connection String -->
<!-- live mode value -->
<add key="ConnectionString"
value="server=192.168.100.3;database=Database_Live;uid=liveUser;pwd=livePassword;"/>
<!-- quality assurance mode -->
<add key="ConnectionString_QA"
value="server=192.168.100.2;database=Database_QA;uid=qaUser;pwd=qaPassword;"/>
<!-- dev mode value -->
<add key="ConnectionString_Dev"
value="server=192.168.100.1;database=Database_Dev;uid=devUser;pwd=devPassword;"/>
<!-- End of Connection String -->
<!-- Start of User Time Out
Specifies how long a user is logged in
-->
<!-- live mode value -->
<add key="User_TimeOut" value="50"/>
<!-- quality assurance mode-->
<add key="User_TimeOut_QA" value="30"/>
<!-- dev mode value -->
<add key="User_TimeOut_Dev" value="10"/>
<!-- End of User Time Out -->
获取要使用哪个设置的逻辑是通过代码结合开关和 appSettings
来完成的。不一定所有设置都需要采用这种格式。如果设置在不同环境中不发生变化,您可以选择不使用开关,并且只有一个条目。
using System.Configuration;
//under class declaration
/// <summary>
/// <para>Get the AppSettings value present in the web.config file.</para>
/// </summary>
/// <param name="key">The key setting in appSettings node.</param>
/// <returns>String containing the value based on Mode Switch</returns>
public static string GetAppSetting(string key)
{
try
{
string returnValue = String.Empty;
//get the value
returnValue = GetAppSetting(key, true);
return returnValue;
}
catch
{
throw;
}
}
/// <summary>
/// <para>Get the AppSettings value present in the web.config file.</para>
/// <para>The useModeSwitch help to obtain values
/// that are common to Dev, QA or LIVE</para>
/// </summary>
/// <param name="key">The key setting in appSettings node.</param>
/// <param name="useModeSwitch">Specifies whether
/// you want to use Live Mode Values</param>
/// <returns>String containing the value based on Mode Switch</returns>
public static string GetAppSetting(string key, bool useModeSwitch)
{
try
{
string returnValue = String.Empty;
//uses modeswitch only if you set to true
//Else gives the Live Mode Values
if (useModeSwitch)
{
//This is done so that the developers can work on their local copy of module
//settings without having to change the original values
//This requires that you have mirror nodes in the web.config, which points to
//your local computer settings.
if (DevModeSwitch.Enabled)
{
key += "_Dev";
}
else if (QAModeSwitch.Enabled)
{
key += "_QA";
}
}
//get the value
returnValue = ConfigurationManager.AppSettings[key];
return returnValue;
}
catch
{
throw;
}
}
以下是 Trace Switch 的一些用法。您可以使用 Trace.Warn
在启用跟踪后在 Live 环境中写入调试信息,或者您可以使用 Debug.Write
在 Development 环境中输出消息,而无需设置断点。
if(DebugSwitch.TraceError)
{
//writing to Dubug window
Debug.Write("Error Line \n");
//writing to the Trace.axd for each page.
//Written in Red color. Use this as its easier to find.
Trace.Warn("Error in Trace");
}
if(DebugSwitch.TraceInfo)
{
//can take any escape sequence for formatting.
Debug.Write("\tHi give me a detailed info of the things to come");
Trace.Warn("Info in Trace");
}
注意事项
- 由于配置文件是为整个应用程序准备的,请尝试将模块特定的设置分解到其自身的配置文件中,并编写自定义配置文件。将此模块配置文件路径作为设置添加到应用程序配置文件中,并从中读取值。
- Web Services 设计器向 web.config 添加了一个用于动态请求的条目。要重现此场景,您需要编辑生成的代理类文件以包含此场景。不幸的是,当您重建 Web Service 引用时,这会丢失。也许在 Visual Studio 的未来版本中,在创建 Web Service 代理类时,选择 URL 的逻辑将留给开发人员。
历史
- 2007 年 6 月 26 日:初始版本。