System.Threading.SynchronizationLockException occurred

Sep 25, 2010 at 9:26 AM

I use Unity2 as Factory for controllers for a website created with Asp.Net MVC2, during initialization of the container (first request intercepted from the BeginRequest event),  when I want to register "extension" interception occurs synchronization error,

could someone explain to me why?

private static m_Lock = new object();
private static m_IsFirstRequest = true;

protected void Application_BeginRequest(object sender, EventArgs args)
{
	if (m_IsFirstRequest)
	{
		lock (m_Lock)
		{
			if (m_IsFirstRequest)
			{
				try
				{
					var container = new UnityContainer();

					container.AddNewExtension<Interception>(); // <-- Here System.Threading.SynchronizationLockException occurred

					... next code here

				}
				catch (Exception ex)
				{
				}
			}
		}
	}
}
Marc

Sep 26, 2010 at 3:44 AM

Is it a first-chance exception or an actual unhandled one? First chance exceptions are expected when calling RegisterInstance; this is well known and not an actual issue except in the debugger.

By the way, why are you doing this in BeginRequest instead of Application_Start? Also, the use of statics could really screw you up in the presence of multiple ASP.NET applications (when the server bounces your HttpApplication, for instance.)

And try { } catch(Exception ex) { } is a really bad idea - you should at least log the captured exceptions or something so you know they're happening.

 

Sep 26, 2010 at 8:42 AM

Thanks for your reply.

This is a first chance exception, only detected in debug mode.

In my web application, I need to access of HttpContext properties during initialization but with Application_Start, with IIS7 the context is not available due to a change of design.

For the try / catch I have deliberately not put the code for logging the issue in this discussion ;)

Marc