The IIS Server maintains a number of Application Pools and each application pool can host one or more web applications. The application pool settings could be altered through IIS Manager (inetmgr). But it would be nice if one could change certain settings from the application pool through C# code. Shown below is the code to change the IdleTimeout for an application pool.
string metabasePath ="IIS://localhost/W3SVC/AppPools";
stringappPoolName ="Classic .NET AppPool";
DirectoryEntry apppools =newDirectoryEntry(metabasePath);
//This throws an exception - AccessViolationException
//DirectoryEntry poolOfInterest = apppools.Children.Find(appPoolName);
foreach (DirectoryEntry p inapppools.Children)
{
if(p.Name == appPoolName)
{
Console.WriteLine(p.Name);
Console.WriteLine(p.InvokeGet("IdleTimeout").ToString());
p.InvokeSet("IdleTimeout",new object[] {2});
Console.WriteLine(p.InvokeGet("IdleTimeout").ToString());
p.CommitChanges();
break;
}
}
Session timeout - Web.config settings are not good enough.
Web Apps that needs to run for long durations under a user identity would face Session timeout issues eventually. I had a situation where the web application I work on was dying out after a while. The users who logged into the system are logged out after 20+ minutes. I thought this has to do with Session getting expired and so I made a change in the sessionState.timeout property in the web.config. But sounds like it is not good enough. The problem was not fixed and I decided I would debug the problem using Visual Studio. But I noticed the debugger no longer breaks after the session has died off. This stuck to me that the pool to which the debugger is attached to is no longer running and there might be a setting for Application Pool which tells the timeout. Then I found the IdleTimeout setting inside Application Pool defaults. The above is shown the code that you can use to change this value. Boom! works like magic.