Thanh Duong

I get an exception 'The type initializer for 'Class' threw an exception' when calling a class through a separate application domain. If i call on the code directly without creating another appdomain then things run fine. This class have a few static members and methods.

Seems like all the public static properties of that class failed to initialize throwing System.TypeInitializationException. Any idea on whats going on here

Thanks.

More example of the pseudo-code:

Create new app domain

create instance of PUB class and unwrap (PUB inherits from ..MarshByRef..)

PUB.method() // call on the method from PUB class, now this will call on other objects to do work, and eventually one of the object threw exception 'The type initializer for 'Class' threw an exception'



Re: Visual C# General The type initializer for 'Class' threw an exception

Peter Ritchie

The message is telling you that the static constructor on Class threw an exception. Maybe you can post the code for the Class static constructor






Re: Visual C# General The type initializer for 'Class' threw an exception

Thanh Duong

The class doesn't have any constructor actually. Just a few static properties and methods

public class X

{

private static QDataTranslation m_dataTranslation = QDataTranslation.Instance;

private static QInternalExternalKeyXref m_keyTranslation = QInternalExternalKeyXref.Instance;

private static int m_cacheSize = 3000;

.

.

.

}

Actually it's failing when i tries to initialize m_keyTranslation. The class design of m_keyTranslation is same as m_dataTranslation, so i'm investigating to see why it failed to initialize keyTranslation QInternalExternalKeyXref.Instance.

QInternalExternalKeyXref initializer could be getting wrong database connection string. Might be bad code on my part, i'll post any update.

Thank you for your help.

LATEST UPDATE: My fault, when running through NUnit, we initialize the environment to a different file, when running through another application, we used another environment file, and this environment file is pointing to bad connection strings.

I went ahead and create a static constructor on class X and stepping through to see which member failed intialization.





Re: Visual C# General The type initializer for 'Class' threw an exception

Peter Ritchie

Having static members means an implicit static constructor will be emitted if one isn't explicitly added.

It's hard to say what could cause that particular message. You could try removing the static members one by one to see if the message goes away.

I don't know if this would cause that exception but static constructors are synchronized with a lock of the type, doing something like lock(typeof(Class)) may cause problems.