Alright, so I'm running into a strange little issue and frankly I'm out of ideas. I wanted to throw this out there to see if I'm missing something that I've done wrong, or if ConcurrentDictionary isn't working correctly. Here's the code:
(Cache is a class containing the static ConcurrentDictionary Keys)
var tmp = Cache.Keys.GetOrAdd(type,
key =>
{
var keys = context.GetKeys(key);
if (keys.Count() == 1)
{
return new KeyInfo
{
Name = keys.First().Name,
Info = key.GetInfo(keys.First().Name)
};
}
return null;
});
if (tmp == null)
Cache.Keys.TryRemove(type, out tmp);
return tmp;
The problem is that occasionally tmp
is null
, causing the TryRemove
line to run, yet the return null;
line above is never hit. Since that return null
is the only thing putting null
into the dictionary and it never runs, how could tmp
ever be null
?
Including the Cache class (SetNames is not used by this code):
public class Cache
{
public static ConcurrentDictionary<Type, Info> Keys = new ConcurrentDictionary<Type, Info>();
public static ConcurrentDictionary<Type, string> SetNames = new ConcurrentDictionary<Type, string>();
}