1

I am working on a Silverlight application which gets data from WCF service. Once I receive the data from the service I am trying to register an instance of an object to the Unity container and I see that it throws ThreadSynchronizationLock exception.

Can anybody help me avoid this exception?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Please include the full exception with stack trace in your question. – Steven Aug 14 '13 at 06:48
  • 1
    What version of Unity are you using? Does this exception occur just during debugging or also at runtime? If during debugging, it should be fixed in later releases: http://entlib.uservoice.com/forums/89245-general/suggestions/2377307-fix-the-system-threading-synchronizationlockexcep . If the issue occurs at runtime, then it's probably because Register methods are not thread-safe so you should synchronize access when performing registrations (after bootstrapping). – Randy Levy Aug 14 '13 at 13:39
  • Yeah we are using 2.0 version of unity. Any way to avoid that? – user2681012 Aug 19 '13 at 05:21
  • Duplicate of http://stackoverflow.com/questions/2873767/can-unity-be-made-to-not-throw-synchronizationlockexception-all-the-time – Nigel Touch Sep 23 '15 at 16:51
  • Apparently fixed in Unity 2.1.505.2 and beyond. [stackoverflow.com/a/11837984](http://stackoverflow.com/a/11837984/97846) – Nigel Touch Sep 23 '15 at 16:53

1 Answers1

0

Known issue. The RegisterInstance method (actually the ContainerControlledLifetimeManager) will try to release a lock it doesn't have when doing RegisterInstance. This is actually a first-chance exception, is properly handled in the codebase, and only shows up in the debugger.

Basically, you don't need to avoid it, it's already handled properly inside the code.

Also, my understanding is that this behavior was changed in Unity 3.0 to avoid exactly these kinds of questions. :-)

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • We are using 2.0 version of unity. And it is happening not only on debugging but also in deployment. Is there a way to avoid this exception? – user2681012 Aug 19 '13 at 05:23
  • Is it a first-chance exception, where it just shows up in the debug output but otherwise keeps running, or is it actually an unhandled exception? It should be the former, and you don't need to avoid it, it's already handled inside the container. If it's actually breaking your process, then something else weird is going on. – Chris Tavares Aug 19 '13 at 06:44