当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《重新启动(回收)应用程序池》经验,为你挑选了6个好方法。

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

感谢您发布示例代码吗?



1> Nathan Ridle..:

开始了:

HttpRuntime.UnloadAppDomain();


这会回收应用程序,但我不确定它是否会回收整个应用程序池(它可以同时托管多个应用程序).
如果您需要远程回收您的Web应用程序非常有用(例如,长寿/单件对象行为不端)

2> dove..:

约翰,

如果您使用的是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进行编辑后,应用程序将重新启动.


哦,继续向他展示如何调整重启.你知道怎么做吗?
嗨,这是一个很老的帖子,但我很难想出一个部分."ServerManagerDemoGlobals.ApplicationPoolArray"来自哪里?即我应该参考什么来访问它?我添加了对Microsoft.Web.Management.dll和Microsoft.Web.Administration.dll的引用谢谢
我添加了对此的引用,它满足属性和propertyBag c:\ windows\SysWOW64\inetsrv\Microsoft.Web.Management.dll(也可在c:\ windows\system32\inetsrv\Microsoft.Web.Management.dll中找到)

3> alexandrul..:

也许这篇文章会有所帮助:

以编程方式回收当前的应用程序池(适用于IIS 6+)

在IIS 6.0中使用WMI回收应用程序池

以编程方式回收IIS 6.0应用程序池

以编程方式回收IIS应用程序池



4> Ricardo Nold..:

下面的代码适用于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();
    }
}

您也可以将"回收"更改为"开始"或"停止".


请注意,您需要在IIS7上启用"IIS 6 WMI兼容性"

5> Spazmoose..:

我使用我的代码稍微不同的路线来回收应用程序池.需要注意的一些事情与其他人提供的不同:

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);
        }
    }
}



6> Wolf5..:

回收在IIS6上运行的代码:

    /// 
    /// Get a list of available Application Pools
    /// 
    /// 
    public static List HentAppPools() {

        List list = new List();
        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;
    }

    /// 
    /// Recycle an application pool
    /// 
    /// 
    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);
    }

推荐阅读
勤奋的瞌睡猪_715
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有