6

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kiquenet
  • 14,494
  • 35
  • 148
  • 243

2 Answers2

5

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

fletcher
  • 13,380
  • 9
  • 52
  • 69
  • If you just need to recycle the services: `iisreset /restart`. And note on Win2k8/Vista and later this needs to be from an elevated command/PowerShell prompt. – Richard Aug 17 '10 at 13:01
  • Personally, I would deploy the files using a WiX installer that will sort all of this stuff out for me and allow a quick deployment to a new server should I need to do so. – fletcher Aug 17 '10 at 13:03
  • @Richard: any sample command/PowerShell? @fletcher: any samples of WIX ? I use Msbuild script for deploy. – Kiquenet Aug 18 '10 at 06:20
  • @alhambraeidos - Wix takes care of the iis restart for you when you create a web site or virtual directory. It's done as part of the installation process. Editing post to include an example – fletcher Aug 18 '10 at 06:31
  • @fletcher: using iisreset command is better than use ServiceController for "World Wide Web Publishing Service" ( "W3SVC" ) ? – Kiquenet Aug 18 '10 at 06:44
  • @alhambraeidos - iisreset does this behind the scenes anyway. Take a look at iisreset.bat to see what it's doing – fletcher Aug 18 '10 at 07:07
  • @alhambraeidos: Just execute iisreset from PSH (`iisreset.exe` is a standard part of Windows when IIS is installed). This is simpler than working out the PSH commands. – Richard Aug 18 '10 at 07:44
3
# 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
    }
}