按环境配置






3.42/5 (6投票s)
根据目标环境自动控制应用程序配置文件内容的自动化方法
引言
除了最小的公司之外,应用程序经常从各种环境迁移,例如:开发、QA 和最终的生产环境。特别大的组织甚至可能有用户验收和/或暂存环境。当应用程序在这些环境中移动时,应用程序的config文件中的元素,例如连接字符串、服务器名称或 Web URL 也必须更改。本文开发了一种系统,其中config文件中的数据可以自动更改以匹配应用程序的目标环境。
背景
我见过开发人员使用各种方法来组织config文件;所有这些方法在完成工作的同时也引入了新的问题。下面给出了两种流行的方法。
注释部分
在这种方法中,应用程序的config文件包含每个环境的重复条目,正确的节保持未注释,而其他节则包含在注释中。这种方法有三个缺点:一是config文件可能会因重复文本而变得非常大。其次,开发人员必须确保更改已正确复制到每个重复的部分。最后,除非自动构建工具解析config文件,否则开发人员必须确保config文件处于正确的状态以便提升到下一个环境。
多个配置文件
在这种方法中,为每个环境创建一个config文件,通常具有像dev.config、prod.config等名称。然后根据需要将这些文件重命名为app.config或web.config。同样,除非自动构建工具重命名config文件,否则开发人员必须手动执行此操作;并且,由于每个文件都包含公共config条目的副本,因此必须确保更改已正确复制到每个文件。
这里介绍的方法是将所有环境通用的配置信息移动到一个单独的文件中,然后通过 XSL 转换将其与特定于环境的信息合并。当作为预构建步骤完成时,此过程将自动为正确的环境创建config文件。
Using the Code
第一步是创建一个 XML 文件,其中包含通用的配置设置。该文件可能有一个空的connectionstrings
部分和所有环境通用的appSettings
。下面显示了名为PreConfig.xml的此类文件的示例。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
</connectionStrings>
<appSettings>
<add key="FormatString" value="{0} {1}"/>
</appSettings>
</configuration>
下一步涉及为每个环境创建一个 XSLT 转换文件。我们将这些文件命名为与我们在 Visual Studio 中使用的Configuration
名称匹配。在此示例中,我们创建了一个Debug文件和一个Release文件。下面显示了调试环境的转换文件Debug.xslt。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="connectionStrings">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<add name="Northwind" connectionString="Data Source=localhost;
Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</xsl:copy>
</xsl:template>
<xsl:template match="appSettings">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<add key="Salutation" value="Greetings from the DEBUG environment"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我们还为发布环境创建了一个转换文件Release.xslt。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="connectionStrings">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<add name="Northwind" connectionString="Data Source=ProdServer;
Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</xsl:copy>
</xsl:template>
<xsl:template match="appSettings">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<add key="Salutation" value="Greetings from the RELEASE environment"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
最后,您将需要一个类似于以下的预构建事件命令
cd $(ProjectDir)
msxsl Preconfig.xml $(ConfigurationName).xslt -o App.config
这些命令假定您正在使用 Microsoft 的命令行转换实用程序msxsl.exe。对于演示项目,该实用程序已包含在项目文件夹中。如果您在其他位置有该工具(或其他实用程序),请根据需要更改预构建事件命令。
关注点
如果您在 Visual Studio 中打开App.config文件,您将在每次构建时都会收到“文件已在源编辑器外部修改”对话框。这可能会很烦人,而且很容易通过不打开文件来避免!
历史
- 2008年6月4日:原始文章