我有以下功能可以停止IIS上的应用程序池,代替托管代码的机器:
/// <summary>Stops the application pool.</summary>
/// <param name="appPoolName">Name of the application pool.</param>
public static void StopAppPool(string appPoolName)
{
ServerManager serverManager = new ServerManager();
ApplicationPool appPool = serverManager.ApplicationPools[appPoolName];
if (appPool != null)
{
if (appPool.State == ObjectState.Started)
{
appPool.Stop();
}
}
}
问题是,对于未托管此代码的计算机,我是如何影响此类应用程序池的?即我们有两个Web服务器,但这个代码只托管在其中一个上。
答案 0 :(得分:1)
如果是一个选项,我会使用Powershell来完成这项任务。在我们的部署过程中,我们使用以下内容:
[HttpPost]
可以像这样创建function Stop-AppPool {
param(
$session = $(throw "session is required"),
[string]$app_pool = $(throw "app_pool is required")
)
Invoke-command -Session $session -ScriptBlock {
param($app_pool)
Import-Module WebAdministration
$appPoolPath = "IIS:\AppPools\" + $app_pool
if (!(Test-Path $appPoolPath -pathType container))
{
Write-Warning("The application pool: $app_pool not found. Operation skipped.")
return
}
$state = Get-WebAppPoolState -Name $app_pool
if ($state.Value -eq 'Stopped')
{
Write-Warning("The application pool: $app_pool is already stopped. Operation skipped.")
return
}
Stop-WebAppPool -Name $app_pool
$retryCount = 0
Do { Start-Sleep -s 5; $retryCount++; $state = Get-WebAppPoolState -Name $app_pool; Write-Host "$app_pool state.value is $($state.Value)" }
While (($state.Value -ne 'Stopped') -and ($retryCount -lt 5))
if ($state.Value -ne 'Stopped') {
$totalSecs = $retryCount * 5
Write-Warning "After $totalSecs $app_pool seems to be still runnnig! The actual state.value is $($state.Value)"
}
} -ArgumentList @($app_pool)
}
:
$session