简体   繁体   English

以编程方式停止和启动IIS。快速安全的方式

[英]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). 我的情况:当我在GAC中部署程序集.NET时,我收到错误(无法访问xxx.dll因为正在使用另一个进程)。 The IIS use those dll (assemblies). IIS使用这些DLL(程序集)。

Which is the best way (more performance,quick and safe way) or all ways to stop, start IIS 6.0 Windows 2003 ? 哪种方法最好(更多性能,快速和安全)或所有方法停止,启动IIS 6.0 Windows 2003? (for C#, .NET 3.5) (对于C#,.NET 3.5)

options, I think: 选项,我认为:

  1. Detect IIS installed in machine. 检测机器中安装的IIS。

  2. Process.Start() using commands: iisreset /stop and iisreset /start Process.Start()使用命令: iisreset /stopiisreset /start

  3. Use ServiceController class for get "World Wide Web Publishing Service" ( "W3SVC" ) and do stop 使用ServiceController类获取“万维网发布服务”(“W3SVC”)并停止

     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) Process.Start()使用命令:taskkill / IM aspnet_wp.exe / F(在Win2003中使用w3wp.exe)

  5. another options that I don't know? 另一种我不知道的选择?

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. 使用iisreset /stop然后在完成部署后iisreset /start将起作用。

It is fairly quick, and ensures a safe restart of IIS. 它相当快,并确保安全重启IIS。

Edit: 编辑:

You can carry out complete configuration of websites and virtual directories using WiX. 您可以使用WiX完成网站和虚拟目录的完整配置。

Wix sample for creating a website in IIS (will not work as is): 用于在IIS中创建网站的Wix示例(不会按原样工作):

<!--  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 http://strangelights.com/blog/archive/2004/10/08/179.aspx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM