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> </p>
<p>Here my modifications (I also attached the modified file)</p>
<p> </p>
<p>private int lockCount;</p>
<p> </p>
<p> /// <summary></p>
<p> /// Retrieve a value from the backing store associated with this Lifetime policy.</p>
<p> /// </summary></p>
<p> /// <returns>the object desired, or null if no such object is currently stored.</returns></p>
<p> /// <remarks>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.</remarks></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> </p>
<p> private void TryExit()</p>
<p> {</p>
<p> try</p>
<p> {</p>
<p> if (lockCount > 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't hold the lock and that's ok.</p>
<p> }</p>
<p> }</p>