TypeInitializationException: The type initializer for '<Module>' threw an exception

You are looking at the bowels of PresentationCore, code written in C++/CLI. The name <Module> is the class name for all global C++/CLI functions. It bombs early, right after the assembly got loaded, the module initializer failed. The exact job done by LoadWpfGfx() isn't documented anywhere that I know but can be easily guessed. It is loading an unmanaged DLL that implements the graphics interface, probably the DirectX interface layer.

This is machine-specific problem. The function checks the registry for the proper DLL to load, then uses LoadLibrary() to load it. Clearly the DLL is missing. Your customer needs to get their machine stable again, then reinstall .NET. If they still have trouble then they can use SysInternals' ProcMon tool to see what registry keys and what DLL are being searched.


This was giving me headache for months. My code was built using Oxygene Pascal compiler, fully integrated with VS 2012. TypeInitializationException happen when the ..cctor fails to do its job somehow.

What did I do to find out why my a C# class lib code runs and my Oxygene class lib code constantly fails when accessed on a WCF web site web service under IIS 7.5 on a remote host, but BOTH worked perfect on a local scale, worked perfectly when doing VS 2012 Unit Testing.

OK, it' is a class library 1. I wrote a class library in CS ToolboxCS.cs 2. I wrote the same class library in Oxygene ToolboxPAS.pas 3. One ToolboxCS runs remotely and ToolboxPAS fails remotely 4. lets see the two codes 4a)

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ToolboxCS
{
    [CompilerGenerated]
    [StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)]
    public static class __Global
    {
        public static string Stop_Time = "1949.Mar.19";
    }
}

4b) the Oxygene sample

namespace ToolboxPAS;
interface
uses
  System;
VAR
  Stop_Time: String := "1949.Mar.19";
implementation
end.

Then I took ILSpy.exe to look at code generated and found that the code disassemble to CS is the same. But looking with ILSPy.exe at the assembly information of the two examples, I could see many differences, some of them had no effect but the one shown below was the killer for my Oxygene ToolboxPAS.pas class lib when it should run on a IIS 7.5 -> w3wp.exe -> xyxyxyxyxy.dll -> ToolboxPAS.dll

{$IFDEF DEBUG}
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default or 
DebuggableAttribute.DebuggingModes.DisableOptimizations or 
DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints or 
DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
{$ENDIF}

In other words, if you do a debug build, your cs code will not show an assembly with this attributes in the assembly info file. But you will see this attributes generated and used looking with ILSpy.exe to the generated DLL.

And after I have added this attributes to my ToolboxPAS.pas Assembly Info file (as shown above) for the debug build it did no more raise InitializationException on a call to access only a simple variable in it.

Since I changed that in all assembly info files for my large DLL class libraries (12 DLL's) they run all like a sniff when deployed to a remote web server IIS 7.5 as WCF wen site web service.