Kiquenet
Kiquenet

Reputation: 15056

Stop and Start IIS programatically. Quickly and safe way

My situation: when I deploy assemblies .NET in GAC, I get errors (Cannot access to xxx.dll because is in use for another process). The IIS use those dll (assemblies).

Which is the best way (more performance,quick and safe way) or all ways to stop, start IIS 6.0 Windows 2003 ? (for C#, .NET 3.5)

options, I think:

  1. Detect IIS installed in machine.

  2. Process.Start() using commands: iisreset /stop andiisreset /start

  3. Use ServiceController class for get "World Wide Web Publishing Service" ( "W3SVC" ) and do stop

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

    and do start

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

  5. another options that I don't know?

Upvotes: 6

Views: 4372

Answers (2)

PowerShellGuru
PowerShellGuru

Reputation: 31

# 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
    }
}

Upvotes: 3

fletcher
fletcher

Reputation: 13790

You don't need to do all of these things.

Using just iisreset /stop then iisreset /start when you have finished your deployment will work.

It is fairly quick, and ensures a safe restart of IIS.

Edit:

You can carry out complete configuration of 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

Upvotes: 5

Related Questions