首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >重新启动(回收)应用程序池

重新启动(回收)应用程序池
EN

Stack Overflow用户
提问于 2008-10-30 11:54:24
回答 7查看 102.6K关注 0票数 72

如何从C# (.net 2)重新启动(回收) IIS应用程序池?

如果你发布示例代码,你会很感激吗?

EN

回答 7

Stack Overflow用户

发布于 2009-07-04 09:45:24

我们开始吧:

代码语言:javascript
复制
HttpRuntime.UnloadAppDomain();
票数 86
EN

Stack Overflow用户

发布于 2009-01-30 17:14:06

下面的代码在IIS6上运行。未在IIS7中测试。

代码语言:javascript
复制
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();
    }
}

您可以将"Recycle“更改为"Start”或"Stop“。

票数 8
EN

Stack Overflow用户

发布于 2015-02-17 09:49:47

我用我的代码走了一条略有不同的路线来回收应用程序池。需要注意的一些事情与其他人提供的内容不同:

1)我使用了一条using语句来确保正确处理ServerManager对象。

2)我正在等待应用程序池在停止之前完成启动,这样我们在尝试停止应用程序时就不会遇到任何问题。类似地,我正在等待应用程序池在尝试启动它之前完成停止。

3)我强制该方法接受一个实际的服务器名称,而不是回退到本地服务器,因为我认为您可能应该知道您正在对哪个服务器运行此命令。

4)我决定启动/停止应用程序,而不是回收它,这样我就可以确保我们不会意外地启动由于其他原因而停止的应用程序池,并避免在尝试回收已经停止的应用程序池时出现问题。

代码语言:javascript
复制
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);
        }
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/249927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档