static contstuctor called multiple times?  
Author Message
arro239





PostPosted: Visual C# General, static contstuctor called multiple times? Top

i guess we've broken smth in our application and now i see a pretty weird thing - static contructor is called twice from the same process

here is the code:

static bool temp = false;

/// <summary>

/// Initializes the static members of this class.

/// </summary>

static WebTransaction()

{

// EXCEPTION_REFACTORING

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.UnhandledException += new UnhandledExceptionEventHandler( MyUnhandledExceptionEventHandler );

// Log the start of the service.

EventLog.Information("Web Transaction Service is starting: " + Process.GetCurrentProcess().Id + " / " + temp);

temp = true;

try

{

//// The data from the client needs to be extracted from the incoming HTTP Stream, uncompressed and deserialized. A

//// Binary Formatter is needed to serialize the Batch structure when the results are sent back to the client.

binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

binaryFormatter.FilterLevel = TypeFilterLevel.Low;

binaryFormatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;

}

catch (Exception exception)

{

EventLog.LogException(exception);

}

}

here is the output:

====
Start of Log Session Monday, December 11, 2006 6:06:34 PM

Log Information : 6:06:34 PM 778 Monday, December 11, 2006
Web Transaction Service is starting: 5768 / False
----
====
Start of Log Session Monday, December 11, 2006 6:06:56 PM

Log Information : 6:06:56 PM 450 Monday, December 11, 2006
Web Transaction Service is starting: 5768 / False
----

same pid - twice :(

what we've recently changed - we've moved from strong named DLLs to unigned DLLs, but i have no idea

how this could be related. Also, the whole thing is running under IIS - it's a IHttpHandler...




Visual C#10  
 
 
boban.s





PostPosted: Visual C# General, static contstuctor called multiple times? Top

From this source i cann't see the problem. Definitely WebTransaction() is called twice. I don't know also what for is temp variable.

 
 
PhilipRieck





PostPosted: Visual C# General, static contstuctor called multiple times? Top

It's possible to have it called twice in the same process if there are multiple app domains. Each app domain that is loading that type will call the class constructor (static constructor)


 
 
arro239





PostPosted: Visual C# General, static contstuctor called multiple times? Top

Definitely WebTransaction() is called twice. I don't know also what for is temp variable.

but how and why is _static constructor_ WebTransaction called twice

the temp variable is a temp variable to check if static constructor is called on the same instance of a class. Two 'false' values tell me the there are two independed versions of the same class in the same process



 
 
arro239





PostPosted: Visual C# General, static contstuctor called multiple times? Top

It's possible to have it called twice in the same process if there are multiple app domains. Each app domain that is loading that type will call the class constructor (static constructor)

what are the reasons for 2 app domains to exist in my IIS enviroment how do i disable this



 
 
PhilipRieck





PostPosted: Visual C# General, static contstuctor called multiple times? Top

I can't really say on this. You said it was a IHttpHandler - is it being used by multiple web apps that are loaded into the same worker process I believe each app gets its own appdomain so this could be the case. In any case, you'll probably be best off asking a followup in the ASP.NET forum. Good luck!
 
 
arro239





PostPosted: Visual C# General, static contstuctor called multiple times? Top

I can't really say on this. You said it was a IHttpHandler - is it being used by multiple web apps that are loaded into the same worker process I believe each app gets its own appdomain so this could be the case. In any case, you'll probably be best off asking a followup in the ASP.NET forum. Good luck!

the reason was my web app was throwing an exception while deploing - this made IIS to add new apps again and again

thanx again, you've pointed me in the right direction