C# (.net 2) から IIS アプリケーション プールを再起動 (リサイクル) するにはどうすればよいですか?
サンプルコードを投稿していただければ幸いです。
C# (.net 2) から IIS アプリケーション プールを再起動 (リサイクル) するにはどうすればよいですか?
サンプルコードを投稿していただければ幸いです。
どうぞ:
HttpRuntime.UnloadAppDomain();
IIS7を使用している場合は、停止していれば実行されます。表示されなくても再起動を調整できると思います。
// Gets the application pool collection from the server.
[ModuleServiceMethod(PassThrough = true)]
public ArrayList GetApplicationPoolCollection()
{
// Use an ArrayList to transfer objects to the client.
ArrayList arrayOfApplicationBags = new ArrayList();
ServerManager serverManager = new ServerManager();
ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
foreach (ApplicationPool applicationPool in applicationPoolCollection)
{
PropertyBag applicationPoolBag = new PropertyBag();
applicationPoolBag[ServerManagerDemoGlobals.ApplicationPoolArray] = applicationPool;
arrayOfApplicationBags.Add(applicationPoolBag);
// If the applicationPool is stopped, restart it.
if (applicationPool.State == ObjectState.Stopped)
{
applicationPool.Start();
}
}
// CommitChanges to persist the changes to the ApplicationHost.config.
serverManager.CommitChanges();
return arrayOfApplicationBags;
}
IIS6を使用している場合はよくわかりませんが、web.config を取得して変更日などを編集してみてください。web.config を編集すると、アプリケーションが再起動します。
たぶん、この記事が役立ちます:
以下のコードは IIS6 で動作します。IIS7 ではテストされていません。
using System.DirectoryServices;
...
void Recycle(string appPool)
{
string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPool;
using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath))
{
appPoolEntry.Invoke("Recycle", null);
appPoolEntry.Close();
}
}
「開始」または「停止」の「リサイクル」も変更できます。
アプリケーション プールをリサイクルするために、コードを少し異なる方法で使用しました。他の人が提供したものとは異なることに注意する必要があります。
1) ServerManager オブジェクトを適切に破棄するために using ステートメントを使用しました。
2) アプリケーション プールを停止する前に、アプリケーション プールの開始が完了するのを待っているため、アプリケーションを停止しようとして問題が発生することはありません。同様に、アプリ プールを開始しようとする前に、停止が完了するのを待っています。
3) メソッドがローカル サーバーにフォールバックするのではなく、実際のサーバー名を受け入れるように強制しています。
4) アプリケーションをリサイクルするのではなく、アプリケーションを開始/停止することにしました。これにより、別の理由で停止したアプリケーション プールを誤って開始しないようにし、既に停止しているアプリケーション プールをリサイクルしようとする際の問題を回避できました。アプリケーション プール。
public static void RecycleApplicationPool(string serverName, string appPoolName)
{
if (!string.IsNullOrEmpty(serverName) && !string.IsNullOrEmpty(appPoolName))
{
try
{
using (ServerManager manager = ServerManager.OpenRemote(serverName))
{
ApplicationPool appPool = manager.ApplicationPools.FirstOrDefault(ap => ap.Name == appPoolName);
//Don't bother trying to recycle if we don't have an app pool
if (appPool != null)
{
//Get the current state of the app pool
bool appPoolRunning = appPool.State == ObjectState.Started || appPool.State == ObjectState.Starting;
bool appPoolStopped = appPool.State == ObjectState.Stopped || appPool.State == ObjectState.Stopping;
//The app pool is running, so stop it first.
if (appPoolRunning)
{
//Wait for the app to finish before trying to stop
while (appPool.State == ObjectState.Starting) { System.Threading.Thread.Sleep(1000); }
//Stop the app if it isn't already stopped
if (appPool.State != ObjectState.Stopped)
{
appPool.Stop();
}
appPoolStopped = true;
}
//Only try restart the app pool if it was running in the first place, because there may be a reason it was not started.
if (appPoolStopped && appPoolRunning)
{
//Wait for the app to finish before trying to start
while (appPool.State == ObjectState.Stopping) { System.Threading.Thread.Sleep(1000); }
//Start the app
appPool.Start();
}
}
else
{
throw new Exception(string.Format("An Application Pool does not exist with the name {0}.{1}", serverName, appPoolName));
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Unable to restart the application pools for {0}.{1}", serverName, appPoolName), ex.InnerException);
}
}
}
IIS6 で動作するコードをリサイクルします。
/// <summary>
/// Get a list of available Application Pools
/// </summary>
/// <returns></returns>
public static List<string> HentAppPools() {
List<string> list = new List<string>();
DirectoryEntry W3SVC = new DirectoryEntry("IIS://LocalHost/w3svc", "", "");
foreach (DirectoryEntry Site in W3SVC.Children) {
if (Site.Name == "AppPools") {
foreach (DirectoryEntry child in Site.Children) {
list.Add(child.Name);
}
}
}
return list;
}
/// <summary>
/// Recycle an application pool
/// </summary>
/// <param name="IIsApplicationPool"></param>
public static void RecycleAppPool(string IIsApplicationPool) {
ManagementScope scope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2");
scope.Connect();
ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/" + IIsApplicationPool + "'"), null);
appPool.InvokeMethod("Recycle", null, null);
}
シンプル・イズ・ベストと感じることもあります。そして、実際のパスを巧妙な方法で適応させて、他の環境でより広い方法で作業することをお勧めしますが、私の解決策は次のようになります。
ExecuteDosCommand(@"c:\Windows\System32\inetsrv\appcmd recycle apppool " + appPool);
C# から、このトリックを実行する DOS コマンドを実行します。上記の解決策の多くは、さまざまな設定で機能しないか、Windows の機能を有効にする必要があります (設定によって異なります)。
このコードは私のために働きます。アプリケーションをリロードするために呼び出すだけです。
System.Web.HttpRuntime.UnloadAppDomain()
別のオプション:
System.Web.Hosting.HostingEnvironment.InitiateShutdown();
UploadAppDomain
前者が作業を完了するのを待っている間にアプリを「終了」するよりも優れているようです。