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.
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.