MrProgram
MrProgram

Reputation: 5242

Refresh IIS site custom settings without restarting it

I have a custom settings file (Settings.xml) where I specify all that I need. When changeing that file while running the website the site, of course, restart.

Settings.xml is read into an object that have the same structure as the xml and then I work with the Settings object

private static readonly XDocument Settings = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml");

The site runs in IIS 8.5.

Is it possible to update Settings.xml without forceing the website to restart?

Is it the IIS that automatically restart?

Upvotes: 0

Views: 870

Answers (1)

teo van kot
teo van kot

Reputation: 12491

You can read your setting on every file change like this:

private static DateTime? _lastSettingRead;
private static XDocument _cahedSettings;
private static XDocument Settings
{
    get
    {
        var settingsPath = AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml";
        //Get last change Settings file datetime
        var lastSettingChange = System.IO.File.GetLastWriteTime(settingsPath);
        //If we read first time or file changed since last read
        if (!_lastSettingRead.HasValue || lastSettingChange > _lastSettingRead)
        {
            _lastSettingRead = lastSettingChange; //change read date
            _cahedSettings = XDocument.Load(settingsPath); //load settings to field
        }
        return _cahedSettings; //return cached settings from field
    }
}

Upvotes: 2

Related Questions