I am having an intermittent issue that is appearing on one server only, and is causing all MVC pages to fail to load with the error \"An item with the same key has already b
We struggled with this issue for 7 months. The issue seemed to come about when our IIS application pools were automatically recycled. Recycling them again or flicking the web.config usually brought the site back online but it took a lot of digging to find the root cause. After assistance from some external consultants and Microsoft tech support, the issue was traced to a bug in the Telerik library where a key is dynamically added to ConfigurationManager.AppSettings without muti-thread race condition protection. In this instance, we were able to bypass the offending code by adding some entries to the web.config. The issue was identified by Telerik several versions ago so we will update to a newer version for a more permanent fix. The problem comes about if you dynamically update the ConfigurationManager.AppSettings. MS suggestion is as follows:
Change this:
try
{
System.Configuration.ConfigurationManager.AppSettings["YourAppSettingsKey"] = string1;
}
catch(System.Exception)
{
}
to this:
if(ConfigurationManager.AppSettings.Get(key) == null)
{
lock (_lockObj)
{
if(ConfigurationManager.AppSettings.Get(key) == null) // check required inside the lock()
{
ConfigurationManager.AppSettings.Set(key, value);
}
}
}
Hope this helps.
If anyone is interested, I've solved this (I think) in a very hacky kind of way... What I've done is wire up to the Application_Error
event in global.asax, and then check to see if the Server.GetLastError()
exception (or its inner exception) contains the details of the error that is thrown (see full stack trace in the question). If it does, I then call for an app domain restart using the method found in this article: http://www.west-wind.com/weblog/posts/2006/Oct/08/Recycling-an-ASPNET-Application-from-within (basically using HttpRuntime.UnloadAppDomain();
or if that doesn't work by touching the web.config file.
I need a shower...