Can Unity be made to not throw SynchronizationLockException all the time?

前端 未结 8 1394
借酒劲吻你
借酒劲吻你 2020-11-29 18:36

The Unity dependency injection container has what seems to be a widely known issue where the SynchronizedLifetimeManager will often cause the Monitor.Exit method to throw a

8条回答
  •  萌比男神i
    2020-11-29 19:02

    I use this short solution:

    /// 
    /// KVV 20110502
    /// Fix for bug in Unity throwing a synchronizedlockexception at each register
    /// 
    class LifeTimeManager : ContainerControlledLifetimeManager
    {
        protected override void SynchronizedSetValue(object newValue)
        {
            base.SynchronizedGetValue();
            base.SynchronizedSetValue(newValue);
        }
    }
    

    and use it like this:

    private UnityContainer _container;
    ...
    _container.RegisterInstance(instance, new LifeTimeManager());
    

    the problem is that the base class of ContainerControlledLifetimeManager expects a SynchronizedSetValue to do a monitor.Enter() via the base.GetValue, however the ContainerControlledLifetimeManager class fails to do this (apparently its developers didn't have 'break at exception' enabled?).

    regards, Koen

提交回复
热议问题