gpt4 book ai didi

c# - 如何使用 MsBuild 以编程方式停止或启动 IIS(6.0 和 7.0)中的网站?

转载 作者:IT王子 更新时间:2023-10-29 04:06:18 24 4
gpt4 key购买 nike

我有 Windows Server 2003 (IIS 6.0) 和 Windows Server 2008 (IIS 7.0) 服务器,我使用 MSBuild 部署 Web 应用程序。

我需要进行安全部署,然后执行此操作:

  1. 停止 IIS 6 中的网站(或 IIS 7 中的应用程序),而不是停止 AppPool。

  2. 检查网站是否停止;没有运行。

  3. 如果网站已停止,请执行另一项部署任务。

  4. 启动网站 IIS 6(或 IIS 7 中的应用程序),

我怎样才能做到这一点?

更新:我的关键:IIS6WebSite 和 IIS6AppPool(对于 IIS7),尝试停止网站或 AppPool 时是否等待停止状态?

当我为网站执行停止操作(或为 AppPool 停止操作)时,我需要确保网站 100% 停止,然后,只有当网站停止时,我才能执行其他目标。

最佳答案

通过添加对 Microsoft.Web.Administration 的引用(可以在 X:\Windows\System32\inetsrv 或您的系统中找到)您可以实现IIS7 对情况的良好管理控制,如下所示:

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!");
}
}
}
}

显然根据您自己的要求对其进行定制,并通过您的部署构建脚本执行生成的应用程序。

享受吧。 :)

关于c# - 如何使用 MsBuild 以编程方式停止或启动 IIS(6.0 和 7.0)中的网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4958799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com