How can I programmatically stop or start a website in IIS (6.0 and 7.0) using MsBuild?
Solution 1
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. :)
Solution 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
Kiquenet
Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts What have you tried? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments Asking http://stackoverflow.com/help/asking Answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers http://www.enriquepradosvaliente.com http://kiquenet.wordpress.com ◣◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤ ◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤◢ .NET developer and fan of continuous self-improvement and good patterns and practices. Stuff I am interested in: .NET technology stack in general, C#, Powershell and Javascript in particular as languages Test driven development, DI, IoC and mocking frameworks Data access with ORMs and SQL ASP.NET javascript, jQuery and related frontend frameworks Open source projects
Updated on May 13, 2020Comments
-
Kiquenet almost 4 years
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:
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.
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.
-
Grant Thomas about 13 yearsJust to note IIS7 has a managed API, rather than relying on
DirectoryServices
orWMI
: learn.iis.net/page.aspx/284/using-managed-apis-in-iis-7 and msdn.microsoft.com/en-us/library/… -
Kiquenet about 13 yearsCan I use Microsoft.Web.Administration for Win2003-IIS 6 and VS 2008 .NET 3.5 ??
-
Kiquenet about 13 yearsany sample msbuild and powershell ?. I need stop and start WebSites in iis6, (or sites in iis7), not appPool.
-
Grant Thomas about 13 yearsNo, this will only work for IIS7. You will need a different approach for IIS6, such as using
WMI
orDirectoryServices
. -
Mike Christensen over 8 yearsNote
Microsoft.Web.Administration
can also be found onNuGet
-
Thameem over 4 yearsDo you have any idea of running the site through winforms cefsharp
-
Rask almost 3 yearsGreat answer. Thanks! In my case I found that I also needed to stop and start the application pool.