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 Flush
ing the current Response
. Is there a better way?