简体   繁体   English

使用C#回收IIS7应用程序池

[英]Recycle IIS7 Application Pool with C#

I have read through all of the threads on here on how to do this programmatically but I can not get it to work. 我已经阅读了有关如何以编程方式执行此操作的所有线程,但无法正常工作。 A little background on how my code works. 关于我的代码如何工作的一些背景知识。

You log in to the site and choose App Pool monitor. 您登录到站点,然后选择“应用程序池”监视器。 There is a drop down that allows you to choose the server you are looking for. 有一个下拉列表允许您选择要查找的服务器。 I have a stored procedure that will pull the app pools from that server and display them in a grid. 我有一个存储过程,它将从该服务器中拉出应用程序池并在网格中显示它们。 I then have this code that I am testing: 然后,我有正在测试的代码:

while (rdr.Read())
{
     string appPool = rdr["AppPoolName"].ToString();
     string permission = rdr["Permission"].ToString();
     string serverIP = rdr["ServerIP"].ToString();

     if (permission == "E")
     {
          lblErrorStart.Visible = false;

          using (var serverManager = ServerManager.OpenRemote(serverIP))
          {
               ApplicationPool poolName = serverManager.ApplicationPools[appPool];
               poolName.Stop();
               lblSuccess.Visible = true;
          }
     }

     else
     {
          lblErrorStart.Visible = true;
     }
}

The reader I have created pools the AppPoolName, ServerIP, and Permission from SQL. 我创建的阅读器汇集了SQL的AppPoolName,ServerIP和Permission。

I use a DirectoryEntry for IIS6 and it works great but it will not work for IIS7. 我将DirectoryEntry用于IIS6,它的工作原理很好,但不适用于IIS7。

using (DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://" + serverIP + "/W3SVC/AppPools/" + appPool)))
{
     w3svc.Invoke("Recycle", null);
     lblSuccess.Visible = true;
}

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: The exception I get with the Directory Services is System.Runtime.InteropServices.COMException 编辑:我与目录服务获得的异常是System.Runtime.InteropServices.COMException

How do you know it's not working? 您怎么知道它不起作用? How would you really know if the app pool has been recycled or not? 您如何才能真正知道应用程序池是否已回收? We had this issue several months ago and tried to figure out a way to see if it's really recycling or not. 几个月前,我们遇到了这个问题,并试图找到一种方法来查看它是否真正在回收。 We turned on tracing in IIS but it didn't log any messages, which we thought meant it wasn't working. 我们在IIS中打开了跟踪功能,但是没有记录任何消息,我们认为这是行不通的。 What we did then, was create a dummy service and a console app that would ping that service every 0.5 a second. 然后,我们要做的是创建一个虚拟服务和一个控制台应用程序,该应用程序每0.5秒钟ping该服务一次。 Then, we recycled the AppPool that was hosting the service, and oddly enough, we saw a delay in the ping response, which meant it actually did recycle. 然后,我们回收了托管服务的AppPool,奇怪的是,我们发现ping响应出现了延迟,这意味着它确实可以回收。

Try changing w3svc.Invoke("Recycle", null); 尝试更改w3svc.Invoke("Recycle", null); to w3svc.Invoke("Stop", null); w3svc.Invoke("Stop", null); . If that stops the AppPool, then you can be almost sure that Recycle will work just fine. 如果那停止了AppPool,那么您几乎可以确定Recycle可以正常工作。 Note that invoking a Recycle operation does not block , so if your code depends on the AppPool to be up and running after a recycle, you need to check for its state before continuing. 请注意,调用Recycle操作不会阻塞 ,因此,如果您的代码依赖于AppPool在回收后启动并运行,则需要在继续之前检查其状态。

Edit: 编辑:

Give this a shot: 试一下:

In the Windows Features window, make sure IIS 6 Management Compatibility is enabled. 在“ Windows Features窗口中,确保启用了IIS 6 Management Compatibility

在此处输入图片说明

Open up the Start menu and type in "services", then press Enter. 打开“开始”菜单并键入“服务”,然后按Enter。 Make sure IIS Admin Service is running. 确保IIS Admin Service正在运行。 Now try to invoke the command and see if it works. 现在尝试调用该命令,看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM