C# Recycle IIS 6’s App Pool error

asp.netcnetweb-applications

I want to Recycle IIS 6's App Pool in a web app using asp.net c#.

protected void Page_Load(object sender, EventArgs e)
{
    //Recycle IIS 6's App Pool
    Recycle("localhost", "appPool_02");

}

void Recycle(string machine, string appPoolName)
{
    string path = "IIS://" + machine + "/W3SVC/AppPools/" + appPoolName;
    DirectoryEntry w3svc = new DirectoryEntry(path);
    w3svc.Invoke("Recycle", null);
}

"appPool_02" is another app pool name, and the above code is running on "appPool_01".

When I used the code above, it occured an error:

拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: 拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:

Line 72:         string path = "IIS://" + machine + "/W3SVC/AppPools/" + appPoolName;
Line 73:         DirectoryEntry w3svc = new DirectoryEntry(path);
Line 74:         w3svc.Invoke("Recycle", null);
Line 75:     }
Line 76: 

Source File: e:\iProject\iProgress\iProgress\T\T.aspx.cs Line: 74

Stack Trace:

[UnauthorizedAccessException: 拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))]

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) +238
WE_T.Recycle(String machine, String appPoolName) in e:\iProject\iProgress\iProgress\T\T.aspx.cs:74
WE_T.Page_Load(Object sender, EventArgs e) in e:\iProject\iProgress\iProgress\T\T.aspx.cs:38
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Best Answer

Your initial post had a space after "localhost" in the machine name. What happens if you remove this?

Also, you'll need to make sure that the ASP.NET user process where this code runs has the appropriate privileges to restart other app pools. You can either use impersonation to do this (bad) or follow the directions in the exception to allow the user process appropriate rights to the other app. pool.