SynchronizationLockException in ConfigureContainer

Topics: Prism v4 - WPF 4
Jul 21, 2011 at 1:22 AM

I have a UnityBootstrapper derived bootstrapper class, with an overridden ConfigureContainer function. When calling this.Container.RegisterInstance or this.Container.RegisterType, I get the following exception message:

A first chance exception of type 'System.Threading.SynchronizationLockException' occurred in Microsoft.Practices.Unity.dll

Additional information: Object synchronization method was called from an unsynchronized block of code.

Any ideas on why this might be happening and how I might go about fixing it?


Developer
Jul 21, 2011 at 5:43 PM

Hi,

We've tried overriding that method and we've experienced no errors. The exception you're experiencing might be happening due to the fact that some code that involves synchronization locks might be placed in your class's constructor, and when you use the RegisterType method, that code is being called, thus throwing the exception.

If you can't find a cause for your problem, it would be helpful if you could provide us with a repro sample (for example, uploading it to SkyDrive), so that we can help you solve this issue.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 18, 2011 at 1:20 AM
Edited Dec 18, 2011 at 1:24 AM

 

Hi!

 

The same...

It's too late, but if I have understood correctly, I have the problem in myBootstrapperGestionAplicacion
Is it right?

The million dollar question. Is this important? How concerned?
thank you very much


    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            BootstrapperGestionAplicacion b = new BootstrapperGestionAplicacion();
            b.Run();
        }
    }
Developer
Dec 19, 2011 at 4:53 PM

Hi,

Based on my understanding, the problem described in this thread, as Guido Maliandi suggested, might be located in the constructor of the class you are trying to register when overriding the ConfigureContainer method, possibly due to some code that involves synchronization locks in it.

Regarding the code snippet you provided, it seems to be a valid approach to initialize your bootstrapper and doesn't seems to be related to the problem.

If you are still experiencing this undesired behavior it would be helpful if you could provide us with a repro sample application, so that we can help you further with this.

Agustin Adami
http://blogs.southworks.net/aadami


Feb 19, 2012 at 5:24 PM

Hi,

I think this is related to a known issue in Unity [running the Modularity with Unity quickstart in debug repros the issue]

see http://unity.codeplex.com/workitem/7019?ProjectName=unity
and http://unity.codeplex.com/discussions/270846
and http://unity.codeplex.com/discussions/51337

A 'handled' exception arises in SynchronizedLifetimeManager.cs:

        private void TryExit()
        {
            try
            {
                Monitor.Exit(lockObj);
            }
            catch (SynchronizationLockException)
            {
                // Noop here - we don't hold the lock and that's ok.
            }
        }

which is called by SetValue.

Monitor.Enter(lockobj) is called by GetValue, but SetValue can be called without a previous call to GetValue with the result that a SynchronizationLockException is generated.

There are some workarounds possible for users of Unity outside of Prism on [see stackoverflow.com].