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

前端 未结 8 1360
借酒劲吻你
借酒劲吻你 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条回答
  • 2020-11-29 18:56

    Unity 2.1 - August 2012 update fix the bug

    1. Addressing a thread safety issue: http://unity.codeplex.com/discussions/328841

    2. Improving debugging experience on System.Threading.SynchronizationLockException: https://entlib.uservoice.com/forums/89245-general/suggestions/2377307-fix-the-system-threading-synchronizationlockexcep

    3. Improving debugging experience through better error messaging when a type cannot be loaded: http://unity.codeplex.com/workitem/9223

    4. Supporting a scenario of performing a BuildUp() on an existing instance of a class that doesn’t have a public constructor: http://unity.codeplex.com/workitem/9460

    To make the update experience as simple as possible for users and to avoid the need for assembly binding redirects, we chose to only increment the assembly file version, not the .NET assembly version.

    0 讨论(0)
  • 2020-11-29 19:02

    I use this short solution:

    /// <summary>
    /// KVV 20110502
    /// Fix for bug in Unity throwing a synchronizedlockexception at each register
    /// </summary>
    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

    0 讨论(0)
提交回复
热议问题