一般来说,在您将 Web 应用 正式部署于生产环境前,它已经被用于多个环境了。这些环境可能包括: 开发人员开发环境、质量保证 (QA) 以及用户验收测试 (UAT)/暂存/预生产环境。在这些环境中转换应用程序时,配置文件中的很多设置都必须更改。比如:数据库链接字符串的配置,文件存放位置,服务器的地址和端口等。
VS2010 中通过提供 XML-Document-Transform engine 来帮我们解决这个问题。
要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空间。如下面代码所示:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
转换的工作原理:
当 Microsoft Visual Studio 2010 编译时,编译器会自动根据当前选择的编译选项,选择对应的web.编译选项名.config和web.config 进行合并转换,继而产生对应部署环境的 web.config 文件,如下图所示:
上图中,我们可以看到有2个关键属性:
- xdt:Transform
- xdt:Locator
这两个属性可以设置的值是一下几种类型:
| 转换 | 描述 |
| xdt:Transform=“Replace” | 替换第一个匹配的节点 |
| xdt:Transform=“Remove” | 清除第一个匹配的节点 |
| xdt:Transform=“RemoveAll” | 清除所有匹配的节点 |
| xdt:Transform=“Insert” | 在末尾插入节点 |
| xdt:Transform=“SetAttributes(attributeNames)” | 创建或更改现有属性的值 |
| xdt:Transform=“RemoveAttributes(attributeNames)” | 清除属性(如果有) |
| xdt:Transform=“InsertBefore(XPath)” | 在指定Xpath前插入节点 |
| xdt:Transform=“InsertAfter(XPath)” | 在指定Xpath后插入节点 |
| 定位符 | 描述 |
| xdt:Locator=“Match(attributeName)” | 可以使用逗号分隔属性名称 |
| xdt:Locator=“Condition(xPath Predicate)” | 可以接受任何 Xpath 谓词,如xdt:Locator="Condition(@name=’Northwind’ or @providerName=’ System.Data.SqlClient’)" |
| xdt:Locator=“Xpath(/configuration/…)” | 可以接受任何复杂的 Xpath,如 "XPath(//system.web)" |
下面是一个简单的实例:
如下图所示,我们在一个 Web 项目中, 配置编译选项:
我们新建一个,按照我们自己发布情况定义的编译选项:
如下图所示,我们建立一个本地部署测试环境的配置, 注意,这里默认就选中了 Create new project configurations
在 Solution Explorer 中选中 Web.config ,在右键菜单中点击:“Add Config Transforms” 项,如下图:
我们就可以看到多了一个“Web.LocalDeploy.config” 文件:
为了测试期间,我们 web.config 文件有以下设置:
<configuration> <appSettings> <add key="KeyOne" value="A value"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.0" />
Web.LocalDeploy.config 文件有以下设置:
<?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="KeyOne" value="B Value" xdt:Transform="Replace" xdt:Locator="Match(key)"/> </appSettings> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration>
从 Web.LocalDeploy.config 文件我们可以看到, 我们使用 LocalDeploy 发布时,要求修改 appSettings 属性 KeyOne 的值,同时,删除 compilation 中打开 debug 的功能。
之后,我们发布项目时, 就会自动产生对应的配置文件,发布选项如下:
为了简单起见,这里是发布到另外一个目录,我们在 D:/123/1 目录下查看 web.config 文件,就可以看到是下面的信息:
<configuration> <appSettings> <add key="KeyOne" value="B Value"/> </appSettings> <system.web> <compilation targetFramework="4.0" />
小结:
通过 ASP.net 4.0 新特性:Web.Config Transformation,我们可以自动产生不同使用环境下的 web.config。很强大,不错。
但是这跟我的期望还是有点差异,比如有些时候,我的配置并不在 web.config 文件中,就使用不到这个新特性。
另外在开发环境模拟部署环境的测试, 还是要手工修改 web.config 文件。而不能力用到这个特性。
本文介绍如何利用ASP.NET 4.0中的Web.Config Transformation特性自动调整Web应用配置,以适应不同环境需求,包括开发、测试及生产环境。

852

被折叠的 条评论
为什么被折叠?



