We have a very serious problem causing thousands of exceptions per minute. We have a website that runs its own caching mechanism, which stores data in the form of:
protected static IDictionary<int, IList<IInterfaceForData>> m_Data = null;
and when we call “Add” in this dictionary, we get a very strange behavior: “ The index was outside the array ” when the key was 100% not in the dictionary:
m_Data.Add(id, new List<IInterfaceForData>());
We protect this call with blocking as follows:
if(Monitor.TryEnter(m_LockObj, 1000))
{
try
{
m_Data.Add(id, new List<IInterfaceForData>());
}
catch(Exception ex)
{
}
finally
{
Monitor.Exit(m_LockObj);
}
}
and we get this exception:
at System.Collections.Generic.Dictionary`2.Resize() at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
We cannot find an explanation, because the exception is related to the security of the dictionary, and we (we think we are) are thread safe. We use lock () or Monitor.TryEnter for each call to Add () and Remove (), except for m_Data.TryGetValue (...)
.
.