cchitsiang
11/12/2013 - 7:58 AM

RecycleAppPool from ASP.NET WebService From http://geekswithblogs.net/svanvliet/archive/2007/01/23/how-to-drop-an-oracle-schema-user-locked-

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.DirectoryServices;
using System.Collections.Generic;
 
[WebService(Namespace = "http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AppServerService : System.Web.Services.WebService
{
    public AppServerService ()
    {
    }
 
    private const string AppPoolDirectoryEntryPath = "IIS://localhost/W3SVC/AppPools";
 
    [WebMethod]
    public List<string> GetAppPoolNames()
    {
        List<string> appPoolList = new List<string>();
 
        using (DirectoryEntry appPoolEntry =
                 new DirectoryEntry(AppPoolDirectoryEntryPath))
        {
            foreach (DirectoryEntry appPool in appPoolEntry.Children)
            {
                appPoolList.Add(appPool.Name);
               
                appPool.Close();
                appPool.Dispose();
            }
        }
 
        return appPoolList;
    }
 
    [WebMethod]
    public AppPoolRestartResponse RecycleAppPool(string appPoolName)
    {
        if (appPoolName == null)
        {
            throw new ArgumentNullException("appPoolName");
        }
 
        AppPoolRestartResponse response = new AppPoolRestartResponse();
        using (DirectoryEntry appPool = new DirectoryEntry(
                 String.Format("{0}/{1}", AppPoolDirectoryEntryPath, appPoolName)))
        {
            try
            {
                appPool.Invoke("Recycle");
 
                response.Successful = true;
                response.Message = String.Format(
                  "Application Pool \"{0}\" was recycled succesfully at {1:HH:mm:ss}.",
                  appPool.Name, DateTime.Now);
            }
            catch (Exception e)
            {
                response.Successful = true;
                response.Message = String.Format(
                  "Application Pool \"{0}\" failed to recycle: \"{1}\".",
                  appPool.Name, e.Message);
            }
        }
 
        return response;
    }
 
    /// <summary>
    ///
    /// </summary>
    public class AppPoolRestartResponse
    {
        private bool _successful;
        private string _message;
 
        public bool Successful
        {
            get { return _successful; }
            set { _successful = value; }
        }
 
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }    
    }
   
}