重新启动(回收)应用程序池
如何从C#(.net 2)重新启动(回收)IIS应用程序池?
感谢您发布示例代码?
约翰,
如果你在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并编辑修改date或其他东西。 一旦对web.config进行编辑,应用程序将重新启动。
开始了:
HttpRuntime.UnloadAppDomain();
也许这些文章将有所帮助:
- 以编程方式回收当前的应用程序池(对于IIS 6+)
- 在IIS 6.0中使用WMI回收应用程序池
- 以编程方式回收IIS 6.0应用程序池
- 以编程方式回收IIS应用程序池
下面的代码在IIS6上工作。 未在IIS7中testing。
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(); } }
您也可以将“回收”更改为“开始”或“停止”。
回收在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); }
我用我的代码稍微不同的路线来回收应用程序池。 有几点需要注意的是与其他人所提供的不同:
1)我使用了一个using语句来确保正确处理ServerManager对象。
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); } } }
有时我觉得那简单是最好的。 虽然我build议人们以一种聪明的方式适应实际的path,以更广泛的方式在其他环境中工作 – 我的解决scheme如下所示:
ExecuteDosCommand(@"c:\Windows\System32\inetsrv\appcmd recycle apppool " + appPool);
从C#中,运行一个DOS命令来完成任务。 上面的许多解决scheme不能在各种设置上运行,并且/或者需要打开Windows上的function(取决于设置)。
这个代码适合我。 只需调用它来重新加载应用程序。
System.Web.HttpRuntime.UnloadAppDomain()