views:

672

answers:

2

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

+1  A: 

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.

Jeremy McGee
Oh, Yes. I remove the space after "localhost", and then the error changes to "Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)"
Mike108
The "RPC server not available" error is not the real error, it is just because there is a space after "localhost" in my initial post. After I remove the space, the real error is Exception from HRESULT: 0x80070005 (E_ACCESSDENIED).
Mike108
A: 

I got it working by setting the AppPool as working as "Local System". But this may cause some security issue. Is there any better solution?

http://stackoverflow.com/questions/1022356/text-to-speech-in-asp-net-access-is-denied-what-to-do

Mike108
The best solution is to never recycle an application pool in this way. Why you need to do that from an ASP.NET application? What if other applications are deployed into the same pool? Your recycle request can break them all.
Lex Li
@Mike108: see this for a better and simpler solution: http://daron.yondem.com/PermaLink.aspx?guid=d82b6b05-8c59-4136-becd-329156083b75
Mauricio Scheffer