Reputation: 3361
I am trying to work out a way to alter the session timeout dynamically, as my customer has requested that this be a value they can easily change (as though it were a database transaction). Here is a truncated version of the value I am trying to change:
<appSettings>
<add key="SessionTimeout" value="20"/>
</appsettings>
I'm working with C#/.Net 3.5 with all of this.
For the record, I am aware that altering the web.config file will cause a restart of the application. What I am going for is to give the administrator of the program the ability to alter the timeout on the fly. If, for example, one of the forms they are requiring people to fill out is taking longer than 20 minutes, and they need to bump the time up to a half hour, or if they want it shortened for some other reason. Since we want to keep the users (including admins) out of the code files, the goal is to create this as part of the site's admin page.
The problem is, I have no idea how to do this, or if it's even possible (much less a good idea). I've given google the run around and come up with nothing.
Is there a way to make these alterations, or even potentially to dynamically change the timeout on the fly (overriding the contents of the web.config)?
Thanks for the help.
Upvotes: 2
Views: 4585
Reputation: 53705
Extend timeout from code where you need it:
Session.Timeout = 40;
Also if you will try to change this parameter in config it will lead to application pool restart.
Upvotes: 1
Reputation: 12063
Instead of trying to change web.config, how about changing Session.Timeout in code at runtime ?
More info :
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout.aspx
Upvotes: 1
Reputation: 2322
Editing your web.config at runtime is possible, but as you have already pointed out, I'm not sure if it is a good idea. There are multiple ways you can go about actually doing the editing of this file, however you have to make sure that the account you are running your application under has the appropriate file system permissions to be able to write to the application directory (eg edit and save the web.config file). If you are in a clustered environment, I would strongly advise not going down this path (though still possible).
Once your permissions etc have been sorted out, below is a quick way of doing it.
public void EditMyWebConfig(string appSettingsKey, string appSettingsValue, string filePath)
{
XmlDocument xml = new XmlDocument();
//Load current config into memory
xml.Load(filePath);
//Overwrite config
using(FileStream fs = new FileStream(filePath, FileMode.Create,FileAccess.ReadWrite))
{
//select appSetting you want to edit
XmlNode xmlN = xml.SelectSingleNode("//configuration/appSettings/add[@key = '" + appSettingsKey + "']");
//assign new value
xmlN.Attributes["value"].Value = appSettingsValue;
//Create new web config
xml.Save(fs);
}
}
I hope this helps, but I think you should just pick a large session time would be easier and cause less problems in the future.
Upvotes: 2