简体   繁体   中英

Recycle App Pool on IIS6 using C#

I'm attempting to recycle an app pool on IIS6 programmatically through a web application. I have searched all over the net and found a bunch of solutions (Most involving impersonation) but none of them seem to work. The most common error I get is E_ACCESSDENIED despite entering a valid username and password. Can anybody point me in the right direction?

Maybe this SO question helps you. There are several solutions (also for IIS6):

IMHO the best you could do is to decide to go with a concrete approach an then when you run into an exception, to ask a concrete question with the source code of your approach. Otherwise it's just very vage to answer your question.

The solution I use for this sort of thing (Where you're trying to run a process from ASP.NET that needs administrative privileges) is the following:

  1. Write whatever you need done as a Self hosted WCF service. Preferably an Http REST Service, so it's easy to call (even using just a browser for testing)
  2. Make sure you service is run using an administrator account. You can use the task scheduler to make sure the service is running at all times as well as run using an Administrator account.
  3. Execute methods on the service from your ASP.NET application using a WCF Client

And it works all the time no matter what "process" I'm trying to run from within an ASP.NET application.

Now as far are the details (code) is concerned let me know if you need help. The code below is the code you'd have in a console application in order to make it a self hosted WCF Service.

In this case it's an Http service listening on port 7654.

static void Main(string[] args)
{
  var webServiceHhost = new WebServiceHost(typeof(AppCmdService), new Uri("http://localhost:7654"));
  ServiceEndpoint ep = webServiceHhost.AddServiceEndpoint(typeof(AppCmdService), new WebHttpBinding(), "");
  var serviceDebugBehavior = webServiceHhost.Description.Behaviors.Find<ServiceDebugBehavior>();
  serviceDebugBehavior.HttpHelpPageEnabled = false;
  webServiceHhost.Open();
  Console.WriteLine("Service is running");
  Console.WriteLine("Press enter to quit ");
  Console.ReadLine();
  webServiceHhost.Close(); 
}

AppCmdService is a WCF Service class that looks like this (in my case). In your case you probably don't need a response from your service. In my case I'm getting a Json response. The actual implementation of what it is you're trying to do will be different obviously. But I'm assuming you already have that piece worked out. So simply call a method of that class from here.

  [ServiceContract]
  public class AppCmdService
  {
    [WebGet(UriTemplate = "/GetCurrentExcutingRequests/?", ResponseFormat= WebMessageFormat.Json)]
    [OperationContract]
    public IEnumerable<ExecutingRequestJson> GetCurrentExcutingRequests()
    {      
      return CurrentExecutingRequestJsonProvider.GetCurrentExecutingRequests("localhost");
    }
  }

On your ASP.NET side, you don't really need a WCF client. All you need is a way to make an http call to the service. So you can simply use HttpWebRequest to make the call out to your service, which in turn execute your process.

Hope all of this makes sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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