Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with the "Microsoft.Practices.EnterpriseLibrary.Caching" when trying to access "CacheFactory.GetCacheManager();"?

Tags:

.net

caching

wcf

What am I doing wrong here or what am I not doing? (I am using this code in a .NET 4.0 WCF Service)

        private static ICacheManager GetCacheManager()
    {
        try
        {                
            return CacheFactory.GetCacheManager();   
        }
        catch (SynchronizationLockException ex)
        {
            EventLogHelper.WriteError(ex);
        }
        catch (ConfigurationException ex)
        {
            EventLogHelper.WriteError(ex);
        }
        return null;
    }

When debugger hits return it throws this exception:

System.Threading.SynchronizationLockException occurred Message=Object synchronization method was called from an unsynchronized block of code. Source=Microsoft.Practices.Unity StackTrace: at Microsoft.Practices.Unity.SynchronizedLifetimeManager.TryExit() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\Lifetime\SynchronizedLifetimeManager.cs:line 109 InnerException:

Is this a bug in Enterprice library?

like image 587
Christofffer Avatar asked Nov 09 '10 12:11

Christofffer


2 Answers

The problem exists in many Enterprise Library blocks indeed. It has something to do with the way Unity is implemented. The actual problem is not that the code itself is incorrect. The exception is catched, but the debugger seems to ignore that fact.

The problem is described here:

  • http://entlib.codeplex.com/workitem/28528
  • http://unity.codeplex.com/workitem/7019?ProjectName=unity

Unfortunately, there is not much you can do to avoid this bug. It would definitely be nice if the Patterns & Practices team should be more iterative in solving this problem ;) .

Happy coding!

like image 84
DotBert Avatar answered Nov 13 '22 21:11

DotBert


It seems that the exception is thrown internally by the Enterprise Library Unity block, however it is also handled as well. This had me pulling my hear out until I realised I had Break when an exception is Thrown enabled for CLR Exception.

Debug -> Exception

Unticking this stopped the debugger from breaking when the exception is thrown.

like image 7
Graham King Avatar answered Nov 13 '22 21:11

Graham King