We have a webfarm with 2 servers, both has the DFS replication implemented. We have a section on homepage which displays 4 articles known as "TopStories" and are cached so that it does not fetches from database everytime
Now when editors updates the Topstories, that change should get live immediately to the end users across the web-farm, now say editor made the changes to the Topstories on server 1 and the end-user who accessed the website was taken to server 2, so user won't be able to see the latest updates on topstories, as on server 2 it will still fetch from cache
So for this we recycles the app-pool by updating the web.config file.
We are using below code to make an update in appsettings element of web.config.public void RefreshWebConfig()
{
//XmlDocument doc = new XmlDocument();
//doc.Load(HttpContext.Current.Server.MapPath("web.config"));
//doc.Save(HttpContext.Current.Server.MapPath("web.config"));
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["TopStorySessionKey"];
int SessionKey = 0;
if (setting != null)
{
SessionKey = Convert.ToInt32(CommonUtil.GetConfigurationValue<int>("TopStorySessionKey"));
if (SessionKey == 100)
SessionKey = 0;
config.AppSettings.Settings["TopStorySessionKey"].Value = Convert.ToString(SessionKey+1);
}
else
{
config.AppSettings.Settings.Add("TopStorySessionKey", SessionKey.ToString());
}
config.Save();
}
I have 2 questions related to this:
- In any instance updating the web.config's app-setting key could delete the whole web.config file using the above code?
- Is there any better way to replicate the change on both the servers.
Thanks in advance.