我如何可以以编程方式停止或使用的MSBuild在IIS(6.0和7.0)启动一个网站? [英] How can I programmatically stop or start a website in IIS (6.0 and 7.0) using MsBuild?
问题描述
我的Windows Server 2003(IIS 6.0)和Windows Server 2008(IIS 7.0)服务器,我用的MSBuild部署Web应用程序。
I have Windows Server 2003 (IIS 6.0) and Windows Server 2008 (IIS 7.0) servers, and I use MSBuild for deploying web applications.
我需要做一个安全的部署,并做到这一点:
I need to do a safe deploy, and do this:
-
停止在IIS 6中的网站(或在IIS 7的应用程序),没有停止的应用程序池。
Stop a website in IIS 6 (or an Application in IIS 7), not stop AppPool.
检查网站被停止;没有运行。
Check if the website is stopped; not running.
如果网站被停止,做部署的另一项任务。
If the website is stopped, do another task for deploy.
启动网站IIS 6(或应用程序在IIS 7中)
Start the website IIS 6 (or Application in IIS 7),
我怎样才能做到这一点?
How can I achieve this?
更新:关键对我来说:?IIS6WebSite和IIS6AppPool(和IIS7),请等待停止状态时,尝试停止网站或程序池
Update: Key for me: IIS6WebSite and IIS6AppPool (and for IIS7), do wait for stopped status when try Stop Website or AppPool?
当我执行停止操作的网站(或停止动作的程序池),我需要的是100%肯定该网站已停止,然后只有当网站停止时,我可以执行其他目标。
When I execute Stop Action for Website (or Stop Action for AppPool), I need be sure 100% that Website is stopped, and then, and only if Website is Stopped, I can execute other targets.
推荐答案
通过添加引用 Microsoft.Web.Administration
(可以在<$ C $找到C> X:\\ WINDOWS \\ SYSTEM32 \\ INETSRV ,或您的系统等价物)可以达到与IIS7的情况很好管理控制,如下采样:
By adding a reference to Microsoft.Web.Administration
(which can be found inX:\Windows\System32\inetsrv
, or your systems equivalent) you can achieve nice managed control of the situation with IIS7, as sampled below:
namespace StackOverflow
{
using System;
using System.Linq;
using Microsoft.Web.Administration;
class Program
{
static void Main(string[] args)
{
var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
if (site != null)
{
//stop the site...
site.Stop();
if (site.State == ObjectState.Stopped)
{
//do deployment tasks...
}
else
{
throw new InvalidOperationException("Could not stop website!");
}
//restart the site...
site.Start();
}
else
{
throw new InvalidOperationException("Could not find website!");
}
}
}
}
显然这量身定制自己的要求,并通过部署构建脚本执行生成的应用程序。
Obviously tailor this to your own requirements and through your deployment build script execute the resulting application.
享受。 :)
这篇关于我如何可以以编程方式停止或使用的MSBuild在IIS(6.0和7.0)启动一个网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!