超棒的Web部署: 如果你在使用XCopy, 那么你就选错了
[原文发表地址] Web Deployment Made Awesome: If You're Using XCopy, You're Doing It Wrong
[原文发表时间] 2010-03-24 10:36 AM
今年我在Mix 10上做过三次演讲,正打算为每个演讲撰写博文,以分享我的演讲和一些有用的代码。
我做了一个关于部署的演讲 "超棒的Web部署 :如果你正在使用 XCopy , 那么你就选错了."
通过以下链接下载演讲,或者在线观看:
视频下载:MP4视频 , Windows Media 视频 , Windows Media视频 ( 高级版 )
我一直会在会议中使用一些比较酷的标题。这至少比"WEB101: Deploying Websites using Microsoft Visual Studio 2010's WebDeploy OneClick Publish Wizard Super Karate Monkey Death Car September CTP R2."来得更好。
以下是我演讲中所涉及的部署相关内容的纲要:
Web 封装—离线与在线
o 通过VS 2010
o 通过IIS 管理
Web.Config 变换
o 变换语法
o 定位器语法
o 为什么不选XSLT?
部署
o 命令行
o what if切换
o 通过IIS
o 内容同步
数据库部署
o 脚本化源数据库
o 添加自定义SQL脚本
开放资源的下载及部署
o Web PI
o 应用程序库
一键发布
o 使用 Web 部署(Ms Deploy) WMSVC
o 使用Web 部署 (Ms Deploy) 远程代理
o 使用 InProc Web 部署 (Ms Deploy)
下面是Visual Studio 2010中关于Web部署的一些很酷的亮点。
在web.config上右击,点击 "Add Config Transforms."。之后会出现一个web.debug.config和一个web.release.config。如果喜欢的话你也可以定制个web.whatever.config,只要名字与配置文件一致即可。而这些文件正好是你想更改的,并不是web.config的完整复件。
也许你想用XSLT转换一个web.config,尽管凭直觉这样可行,但实际却是非常繁琐。
通常有两种转换,一个使用XSLT,另一个使用XML Document Transform语法/命名空间。在XSLT中有多种方法都可以这样做,但必须要知道大体的概念。XSLT是一个广义树的转换语言,而部署是一种常见的场景特定子集优化。而最酷的部分是每个XDT转换都是一个.NET插件,因此可以自己定制。
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/configuration/appSettings">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:element name="add">
<xsl:attribute name="key">NewSetting</xsl:attribute>
<xsl:attribute name="value">New Setting Value</xsl:attribute>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者通过部署转换来得到相同的结果:
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add name="NewSetting" value="New Setting Value" xdt:Transform="Insert"/>
</appSettings>
</configuration>
为了具体地阐述这个问题,以下是一个新的NerdDinner web.debug.config:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<customErrors mode="Off" xdt:Transform="Replace">
</customErrors>
</system.web>
</configuration>
下面是web.release.config。注意我更新了connectionStrings,修改了appSettings(本例中是Twitter的库材料),并且修改了system.web部分,开启customErrors,也删除了调试属性。
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="ApplicationServices" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" connectionString="foobar" providerName="System.Data.SqlClient" />
<add name="NerdDinnerEntities" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" connectionString="foofoo" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="twitterConsumerKey" value="#$@*)$(@*$)@(*)$(#@*"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key="twitterConsumerSecret" value="2340928402"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
<system.web>
<customErrors mode="On" defaultRedirect="/Dinners/Trouble"
xdt:Transform="Replace">
<error statusCode="404" redirect="/Dinners/Confused" />
</customErrors>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
这仅是一个配置转换,它是整个部署过程的一小部分。同时我也给出了打包过程和打包部署,它可能会在Visual Studio的一键部署中出现,或者从IIS启动,或者从你的Continuous Integration方案的命令行启动。
WebDeploy打包和部署解决方案同时也是Web 平台安装所使用的东西。它们都基于相同的引擎。这个截图是我直接从一个压缩文件中导入的一个开源应用程序。注意这不仅是使用什么文件,也是设置ACLs(访问控制列表或权限)和创建一个IIS应用程序。这只涉及到了表层的东西。
请查阅我的演讲,希望对你们有所帮助。那个六十分钟的演讲所涉及的内容比这博文里的内容要更为全面。