开发者

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

开发者 https://www.devze.com 2023-01-23 23:50 出处:网络
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()

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)
        {
         开发者_C百科   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?


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!


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.


I get the same error when trying to create a database in a WCF service. (Entlib 5.0, .net 4.0)

Database db = DatabaseFactory.CreateDatabase();

We got around it by using a SqlDatabase object instead. Database may have been deprecated so that's what I would check first in your code and make sure what you are trying to do is still supported.


I encountered this when adding a feature to an old program that uses EntLib. Turning off the debugger exception felt like a hack. In VS 2015, the Tools | Options | Debugging | Enable Just My Code options worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消