0

I am running into a situation with my Web API where the only option I can think of right now is to recycle the app pool while I dig through and find out the real cause of the problem. In the mean time I need a way to recycle the app pool after a condition is met. I thought I found the solution here: http://www.codeproject.com/Tips/780076/Recycle-Application-Pool-s-in-IIS-Server-Using-C

// Needs Microsoft.Web.Administration NuGet
public static void RecycleAppPool()
{
    ServerManager serverManager = new ServerManager();
    ApplicationPool appPool = serverManager.ApplicationPools["Test_AppPool"];

    if (appPool != null)
    {
        if (appPool.State == ObjectState.Stopped)
        {
            appPool.Start();
        }
        else
        {
            appPool.Recycle();
        }
    }
}

There are 2 problems with this:

1) I am getting an error when trying to run this code: "Filename: redirection.config Error: Cannot read configuration file due to insufficient permissions" I am getting this error in localhost, but I assume I'll also get it in my hosted Azure environment? Can I get around this?

2) The second problem is I may not know what the name of the app pool is, so I need to figure that out programatically. I've found something like this:http://www.logue.com.ar/blog/2008/02/find-and-recycle-current-application-pool-programmatically-for-iis-6/ but that uses ActiveDirectory and I will not have the login details for the AD at my host... at least not that I know of... again this is hosted in an Azure Website.

So the question is, how do I get the name and of the current app pool in Azure and recycle it?

As a side note, I'm doing this in my Global.asax in the Application_EndRequest after Flushing the current Response. Is there a better way?

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183

1 Answers1

0

So far the closest I could find is (found here: https://stackoverflow.com/a/1081902/550975)

HttpRuntime.UnloadAppDomain();

which just restarts the app, not the app pool. This may however serve my purposes... we'll see. I'm still hoping someone has the actual answer, and I will mark it as the Accepted Answer if such a thing ever occurs.

Community
  • 1
  • 1
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183