Stop and start IIS programmatically. Fast and safe

My situation: when I deploy .NET assemblies in the GAC, I get errors (cannot access xxx.dll because it is used for another process). IIS use these dlls (assemblies).

What is the best way (higher performance, faster and safer way) or all the ways to stop running IIS 6.0 Windows 2003? (for C #, .NET 3.5)

I think:

  • Discovery of IIS installed on the computer.

  • Process.Start() using the commands: iisreset /stop and iisreset /start

  • Use the ServiceController class to get the "World Wide Web Publishing Service" ("W3SVC") and stop

     controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(timeoutSeconds)); 

    and start

     controller.Start(); controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(timeoutSeconds)); 
  • Process.Start() with the command: taskkill / IM aspnet_wp.exe / F (use w3wp.exe in Win2003)

  • other options that I don't know?

+6
iis iis-6
source share
2 answers

You do not need to do all this.

Using only iisreset /stop , then iisreset /start , when you finish the deployment, will work.

It is fairly fast and provides a secure restart of IIS.

Edit:

You can fully configure websites and virtual directories using WiX.

Wix sample for creating a website in IIS (will not work as is):

 <!-- Create the web site in IIS --> <Component Id="WebSiteComponent" Guid="<INSERT-GUID>" KeyPath="yes"> <iis:WebAppPool Id="WebSiteAppPool" Name="WebSiteAppPool" RecycleMinutes="1740" QueueLimit="4000" IdleTimeout="20" MaxWorkerProcesses="1" Identity="networkService" /> <!-- web site --> <iis:WebSite Id="WebSiteIIS" AutoStart="yes" ConfigureIfExists="yes" Description="WebSite" SiteId="59" StartOnInstall="yes" Directory="SiteFolder"> <!-- Host headers to enable web site to be hosted on port 80 --> <iis:WebAddress Id="HostHeader" Header="myWebSite" IP="*" Port="80" Secure="no" /> <iis:WebAddress Id="SecureHostHeader" Header="myWebSite" IP="*" Port="443" Secure="yes" /> <!-- download web site web application --> <iis:WebApplication Id="WebSiteWebApplication" AllowSessions="yes" SessionTimeout="20" Buffer="yes" ParentPaths="no" ClientDebugging="no" Name="Default Application" WebAppPool="WebSiteAppPool" DefaultScript="VBScript" ScriptTimeout="90" ServerDebugging="no" /> <iis:WebDirProperties Id="WebSiteProperties" Read="yes" LogVisits="yes" Index="yes" Execute="no" Write="no" AnonymousAccess="yes" AccessSSL="no" Script="yes" AspDetailedError="yes" /> <!-- web service virtual directory --> <iis:WebVirtualDir Id="WebServiceVDir" Alias="Service" Directory="WebServiceFolder"> <iis:WebDirProperties Id="WebServiceVDirProperties" Read="yes" Write="yes" LogVisits="yes" Index="yes" BasicAuthentication="yes" AnonymousAccess="no" AccessSSL="yes" AccessSSL128="yes" Execute="no" Script="yes" AspDetailedError="yes" /> <iis:WebApplication Id="WebServiceWebApplication" AllowSessions="yes" Buffer="yes" ClientDebugging="no" ServerDebugging="no" WebAppPool="WebSiteAppPool" Name="Default Application" SessionTimeout="20" ParentPaths="no" DefaultScript="VBScript" ScriptTimeout="90" /> </iis:WebVirtualDir> </iis:WebSite> </Component> 

For another example, see here:

http://strangelights.com/blog/archive/2004/10/08/179.aspx

+5
source share
 # IISReset.ps1 - using PowerShell [array] $services = ('W3SVC','SMTPSVC','IISAdmin') foreach($service in $services) { $tst = Get-Service $service -ErrorAction SilentlyContinue if($tst -ne $null) { Write-Host $service Stop-Service -Name $service } } [array]::Reverse($services) foreach($service in $services) { $tst = Get-Service $service -ErrorAction SilentlyContinue if($tst -ne $null) { Write-Host $service Start-Service -Name $service } } 
+2
source share

All Articles