Вопрос

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.

I use a DirectoryEntry for IIS6 and it works great but it will not work for 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

Это было полезно?

Решение

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. What we did then, was create a dummy service and a console app that would ping that service every 0.5 a second. 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.

Try changing w3svc.Invoke("Recycle", null); to w3svc.Invoke("Stop", null);. If that stops the AppPool, then you can be almost sure that Recycle will work just fine. 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.

Edit:

Give this a shot:

In the Windows Features window, make sure IIS 6 Management Compatibility is enabled.

enter image description here

Open up the Start menu and type in "services", then press Enter. Make sure IIS Admin Service is running. Now try to invoke the command and see if it works.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top