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
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