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
Unity 2.1 - August 2012 update fix the bug
Addressing a thread safety issue: http://unity.codeplex.com/discussions/328841
Improving debugging experience on System.Threading.SynchronizationLockException: https://entlib.uservoice.com/forums/89245-general/suggestions/2377307-fix-the-system-threading-synchronizationlockexcep
Improving debugging experience through better error messaging when a type cannot be loaded: http://unity.codeplex.com/workitem/9223
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.
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