简体   繁体   English

使用C#在IIS6上回收应用程序池

[英]Recycle App Pool on IIS6 using C#

I'm attempting to recycle an app pool on IIS6 programmatically through a web application. 我正在尝试通过Web应用程序以编程方式在IIS6上回收应用程序池。 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. 尽管输入了有效的用户名和密码,但我遇到的最常见错误是E_ACCESSDENIED。 Can anybody point me in the right direction? 有人能指出我正确的方向吗?

Maybe this SO question helps you. 也许这样的问题可以帮助您。 There are several solutions (also for IIS6): 有几种解决方案(也适用于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: 我用于此类事情的解决方案(在此处尝试从ASP.NET运行需要管理特权的进程)是:

  1. Write whatever you need done as a Self hosted WCF service. 编写您需要做的任何事情作为自托管WCF服务。 Preferably an Http REST Service, so it's easy to call (even using just a browser for testing) 最好是Http REST服务,因此调用起来很容易(即使仅使用浏览器进行测试)
  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 使用WCF客户端从ASP.NET应用程序在服务上执行方法

And it works all the time no matter what "process" I'm trying to run from within an ASP.NET application. 无论我试图从ASP.NET应用程序中运行哪个“进程”,它始终有效。

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. 下面的代码是您在控制台应用程序中拥有的代码,以便使其成为自托管的WCF服务。

In this case it's an Http service listening on port 7654. 在这种情况下,它是在端口7654上侦听的Http服务。

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). AppCmdService是一个WCF服务类,看起来像这样(以我为例)。 In your case you probably don't need a response from your service. 就您而言,您可能不需要服务的回应。 In my case I'm getting a Json response. 就我而言,我得到了Json的回应。 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. 在ASP.NET方面,您实际上并不需要WCF客户端。 All you need is a way to make an http call to the service. 您所需要的只是对服务进行http调用的一种方法。 So you can simply use HttpWebRequest to make the call out to your service, which in turn execute your process. 因此,您可以简单地使用HttpWebRequest来调用您的服务,从而执行您的过程。

Hope all of this makes sense? 希望所有这些都有意义吗?

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

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