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

How can I programmatically stop or start a website in IIS (6.0 and 7.0) using MsBuild?(如何使用 MsBuild 以编程方式在 IIS(6.0 和 7.0)中停止或启动网站?)
本文介绍了如何使用 MsBuild 以编程方式在 IIS(6.0 和 7.0)中停止或启动网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  1. Stop a website in IIS 6 (or an Application in IIS 7), not stop AppPool.

  2. Check if the website is stopped; not running.

  3. If the website is stopped, do another task for deploy.

  4. Start the website IIS 6 (or Application in IIS 7),

How can I achieve this?

Update: Key for me: IIS6WebSite and IIS6AppPool (and for IIS7), do wait for stopped status when try Stop Website or AppPool?

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.

解决方案

By adding a reference to Microsoft.Web.Administration (which can be found inX:WindowsSystem32inetsrv, 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.

Enjoy. :)

这篇关于如何使用 MsBuild 以编程方式在 IIS(6.0 和 7.0)中停止或启动网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)