38

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • I believe what you're looking for is something like [MSBuild Extension Pack](http://msbuildextensionpack.codeplex.com/). It provides tasks to start/stop IIS web sites (for II6 see [Iis6WebSite](http://www.msbuildextensionpack.com/help/4.0.2.0/html/2849df01-25a8-6f99-5a0c-0fa7a6df5084.htm)). – Scott Saad Feb 10 '11 at 15:34
  • +1 while everything here "works", this is the best way if you are just using MSBuild. Why reinvent the wheel? – Taylor Bird Feb 10 '11 at 16:17
  • Key for me: Iis6WebSite and IIs6AppPool (and for IIS7), do wait for stopped status when try Stop WebSite or AppPool ? – Kiquenet Feb 11 '11 at 10:27
  • Using MSBuild Extension Pack., how can I know the status (Started, Stopped, ...) of WebSite or AppPool (IIS6) ?? – Kiquenet Feb 11 '11 at 10:45
  • @alhambraeidos - One option is to use the HttpWebRequest task to see if you get a 404 back (which I guess would assume the site is down/stopped)? – Scott Saad Feb 11 '11 at 17:56

3 Answers3

56

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.

Enjoy. :)

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
2
  • Write a script, e.g. PowerShell, which will stop/start IIS web site programmatically relying on command-line argument, e.g. start-stop.ps1 /stop 1

  • Put it into MsBuild script as a custom step


Check this to find out how to restart IIS AppPool

IIS WMI objects reference

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    Just to note IIS7 has a managed API, rather than relying on `DirectoryServices` or `WMI`: http://learn.iis.net/page.aspx/284/using-managed-apis-in-iis-7/ and http://msdn.microsoft.com/en-us/library/microsoft.web.administration(v=vs.90).aspx – Grant Thomas Feb 10 '11 at 15:31
  • any sample msbuild and powershell ?. I need stop and start WebSites in iis6, (or sites in iis7), not appPool. – Kiquenet Feb 11 '11 at 07:21
0

So you have your answer above for IIS7. What you're missing is IIS6. So here you go. This is using a COM interop object as that's all that is available for IIS 6. Also, because it's in vb, you'll have to figure out how to convert it. http://www.codeproject.com/Articles/16686/A-C-alternative-for-the-Visual-Basic-GetObject-fun should get you on the right track. you could also create a vb project just for this code but that's kind of silly.

Dim WebServiceObj As Object
dim IisSiteId as Integer = 0
WebServiceObj = GetObject("IIS://localhost/W3SVC/" & IisSiteId)
WebServiceObj.Stop()
WebServiceObj.Start()
Richard Barker
  • 1,161
  • 2
  • 12
  • 30