23

Closed

SynchronizedLifetimeManager - SynchronizationLockException

description

<p>The is an easy way to get rid of the exception thrown/handled by SynchronizationLockException.</p> <p>I ran the unit test, every test pass and the exception is never fired.</p> <p>Basicly, I added a counter to validate that the Monitor.Exit() is required to prevent the exception being fired.</p> <p>I understand the exception is currently handled and not throw.</p> <p>But its annoying to always have to disable that kind of exception while debugging with exception catching on.</p> <p>Also it spam the debug log.</p> <p>&nbsp;</p> <p>Here my modifications (I also attached the modified file)</p> <p>&nbsp;</p> <p>private int lockCount;</p> <p>&nbsp;</p> <p> /// &lt;summary&gt;</p> <p> /// Retrieve a value from the backing store associated with this Lifetime policy.</p> <p> /// &lt;/summary&gt;</p> <p> /// &lt;returns&gt;the object desired, or null if no such object is currently stored.&lt;/returns&gt;</p> <p> /// &lt;remarks&gt;Calls to this method acquire a lock which is released only if a non-null value</p> <p> /// has been set for the lifetime manager.&lt;/remarks&gt;</p> <p> public override object GetValue()</p> <p> {</p> <p> Monitor.Enter(lockObj);</p> <p> Interlocked.Increment(ref lockCount);</p> <p> object result = SynchronizedGetValue();</p> <p> if (result != null)</p> <p> {</p> <p> Interlocked.Decrement(ref lockCount);</p> <p> Monitor.Exit(lockObj);</p> <p> }</p> <p> return result;</p> <p> }</p> <p>&nbsp;</p> <p> private void TryExit()</p> <p> {</p> <p> try</p> <p> {</p> <p> if (lockCount &gt; 0)</p> <p> {</p> <p> Interlocked.Decrement(ref lockCount);</p> <p> Monitor.Exit(lockObj);</p> <p> }</p> <p> }</p> <p> catch (SynchronizationLockException)</p> <p> {</p> <p> // Noop here - we don&#39;t hold the lock and that&#39;s ok.</p> <p> }</p> <p> }</p>

file attachments

Closed Aug 7, 2012 at 1:06 AM by
Fixed

comments

rorymacleod wrote Jul 16, 2010 at 10:45 AM

I had the same problem, but I wanted to solve it without creating my own version of Unity. My approach is here: http://stackoverflow.com/questions/2873767/can-unity-be-made-to-not-throw-synchronizationlockexception-all-the-time

ericis wrote Jan 5, 2011 at 11:46 PM

IMHO: I propose a more Agile release of Enterprise Library where smaller releases are made with these types of bug fixes. So far, most versions of the library have been major releases with a lot of time between. The slow release for major versions is nice for enterprises, but bug fixes need to go in faster (like all the WPF Validation integration gotcha's I've been experiencing). For those interested, the Duplicate bug more has a lot of comments: http://unity.codeplex.com/workitem/7019. How an earlier bug is called a duplicate of a later bug, I'm not certain...

tschaena wrote May 24, 2012 at 4:13 PM

When is this bug going to be fixed? It is really annoying that such a clear bug is not going to be fixed!

gmelnik wrote Jul 9, 2012 at 10:29 PM

This is on the backlog - will fix in the next rev of Unity.

iseric wrote Jul 11, 2012 at 12:34 AM

Yea!!!!!!

gmelnik wrote Aug 7, 2012 at 1:00 AM

Fixed in the latest release of Unity (2.1.505.2). Get it via NuGet.
http://bit.ly/Ot1yLj