From: <fli...@us...> - 2008-12-17 04:01:03
|
Revision: 196 http://structuremap.svn.sourceforge.net/structuremap/?rev=196&view=rev Author: flimflan Date: 2008-12-17 04:00:59 +0000 (Wed, 17 Dec 2008) Log Message: ----------- Attempt to resolve StructureMapException 400 when attempting to retrieve the same instance via ObjectFactory.GetInstance on multiple threads. Likely to occur in ASP.NET scenarios (retrieving controllers) Modified Paths: -------------- trunk/Source/StructureMap/Util/Cache.cs Modified: trunk/Source/StructureMap/Util/Cache.cs =================================================================== --- trunk/Source/StructureMap/Util/Cache.cs 2008-11-19 19:10:25 UTC (rev 195) +++ trunk/Source/StructureMap/Util/Cache.cs 2008-12-17 04:00:59 UTC (rev 196) @@ -15,6 +15,7 @@ private readonly Dictionary<KEY, VALUE> _values = new Dictionary<KEY, VALUE>(); private Func<VALUE, KEY> _getKey = delegate { throw new NotImplementedException(); }; + private readonly object _valuesLock = new object(); public Cache() { @@ -70,14 +71,7 @@ public void Store(KEY key, VALUE value) { - if (_values.ContainsKey(key)) - { - _values[key] = value; - } - else - { - _values.Add(key, value); - } + _values[key] = value; } public void Fill(KEY key, VALUE value) @@ -94,8 +88,15 @@ { if (!_values.ContainsKey(key)) { - VALUE value = _onMissing(key); - _values.Add(key, value); + lock (_valuesLock) + { + if (!_values.ContainsKey(key)) + { + // Potential deadlock if _onMissing attempts to retrieve the same key + VALUE value = _onMissing(key); + _values.Add(key, value); + } + } } return _values[key]; @@ -154,10 +155,7 @@ public void Remove(KEY key) { - if (_values.ContainsKey(key)) - { - _values.Remove(key); - } + _values.Remove(key); } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |