Hi,
I have just been working on an inherited c# booking system solution which uses a Utility class with mainly all static functions. One function is defined as below. Some users are getting the error "An item with the same key has already been added". Can anyone see where this may be happening in the multi-user environment? There is only one version ofdicConfiguration so I wondered it must be a timing issue. Just looking at issues others have had I should be putting in a pre-check fordicConfiguration.ContainsKey(cConfiguration.key). Not sure this is what I want - I am wondering why you would want to read the configuration items in again when they have already been loaded. However, don't understand why error is sporadic. I am guessing two users are calling this function. One of them has partially updated the dictionary and the other is trying to update it. Therefore possibly the key test is required - still reads all config list items????
This essentially reads all configuration items in a Custom List - like a settings.ini (old school)
public static Dictionary<string, string> dicConfiguration = new Dictionary<string, string>();
public static bool ReadConfiguration(string Url, bool bRefresh = false)
{
bool bRet = false;
try
{
if (bRefresh)
dicConfiguration.Clear();
if (dicConfiguration == null || dicConfiguration.Count == 0)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList spList = web.Lists.TryGetList("Configuration");
if (spList != null)
{
SPListItemCollection items = spList.Items;
foreach (SPListItem item in items)
{
clsConfiguration cConfiguration = new clsConfiguration(item);
dicConfiguration.Add(cConfiguration.key, cConfiguration.Title);
}
bRet = true;
}
else
{
//this message cannot use Configuration list of course
HandleException("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]" + " list with title Configuration not found");
}
}
}
});
}
}
catch (Exception ex)
{
HandleException(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
}
return bRet;
}