TypeInitializationException: type initializer for '<Module>' threw an exception
I searched googled and debugged widely, and I can't figure out what causes this exception. Anecdotal reinstallation of the .NET framework sometimes fixes this, but seems a bit sketchy as a fix:
TypeInitializationException: The type initializer for '<Module>' threw an exception. The C++ module failed to load during appdomain initialization. The system cannot find the file specified StackTrace: at CModuleInitialize.LoadWpfGfx(CModuleInitialize* ) at CModuleInitialize.{ctor}(CModuleInitialize* ) at ?A0x828d0bb1.??__E?A0x828d0bb1@cmiStartupRunner @@YMXXZ() at _initterm_m((fnptr)* pfbegin, (fnptr)* pfend) at <CrtImplementationDetails>.LanguageSupport.InitializePerAppDomain(LanguageSupport* ) at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) Any clues?
Context
This is a WinForms application written in C # with several new components written in WPF. The crash reports we receive do not include more context than what I am showing (stack trace, etc.), and we do not do any C ++, do not manage it or otherwise.
I could not reproduce this crash, but remote diagnostics of several clients suggests that reinstalling the .NET platform may solve this problem for this client.
Clients who report this failure do this several times, and it seems that they cannot use the application until the .NET platform is re-installed, or they revert to an older version of the application that only used it. NET 2.0
References
Some links point to a possible connection between the beta versions of the .NET Framework or visual studio and this failure, but this does not seem likely in this case.
Another possible connection might be with the redistributable Visual C ++ 2005.
You look at the insides of PresentationCore, the code is written in C ++ / CLI. The name <Module> is the class name for all global C ++ / CLI functions. The bomb early, immediately after loading the assembly, the module initializer failed. The exact work done by LoadWpfGfx () is not documented anywhere I know, but it's easy to guess. It loads an unmanaged DLL that implements a graphical interface, possibly the DirectX interface layer.
This is a machine-specific problem. The function checks the registry for the correct DLL to load, then uses LoadLibrary () to load it. Obviously the dll is missing. Your client needs to reinstall the stability of their machine, and then reinstall .NET. If they still have problems, they can use the SysInternals ProcMon tool to see which registry keys and which DLLs they are looking for.
This gave me a headache for several months. My code was created using the Oxygene Pascal compiler, fully integrated with VS 2012. A TypeInitializationException occurs when ..cctor does not do its work in some way.
What I did to find out why my C # class code works and my Oxygene class lib code constantly fails when accessing the WCF website in IIS 7.5 on a remote host, but BOTH works fine on local scalability, worked great when conducting VS 2012 unit testing.
OK, this 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 is launched remotely and ToolboxPAS is not deleted remotely 4. allows you to see 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) Oxygene sample
namespace ToolboxPAS; interface uses System; VAR Stop_Time: String := "1949.Mar.19"; implementation end. Then I took ILSpy.exe to look at the generated code and found that the disassembly code in CS is the same. But, looking with the help of ILSPy.exe on the assembly of the two examples, I could see many differences, some of them had no effect, but the one shown below was the killer of my Oxygene ToolboxPAS.pas class when it should run on 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 build a debug assembly, your cs code will not show the assembly with these attributes in the assembly information file. But you will see that these attributes are generated and used in the search using ILSpy.exe in the generated DLL.
And after I added these attributes to my ToolboxPAS.pas Assembly Info file (as shown above) for debugging assembly, it no longer raised the value of InitializationException when called to access a simple variable in it.
Since I changed this in all the assembly information files for my large DLL class libraries (12 DLLs), they run everything as sniff when deployed to a remote IIS 7.5 web server as a WCF website web service.
I had the same error message. Component I used the required .net framework 3.0, but my application was intended for 2.0. Users with 3.0 installed on computers had no problems launching my application, but those who couldn’t run it.
Not a problem that many expect, but it is possible.
For me, an error occurred when I had a static variable inside the class that was initialized using the value from the configuration file.
The configuration file did not have a value that threw an exception during class initialization.