Ошибка the type initializer for module threw an exception

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.

  • Remove From My Forums
  • Question

  • Hi All

    I have .Net solution with bunch of c++ and c# projects with .In Debug mode the application  is working fine.When I build the solution in release mode and try to run the application I am getting the strange error «The the type initializer  for <Module> threw an excpetion  The C++ Module failed to load during   native initialization».The problem with it is not throwing any error in debug mode,It was happening only in release mode,Can any aware of this problem?

Answers

  • Sounds like your release build isn’t copying all the files it needs to run.  Verify that all the binaries that are needed are in the appropriate directory.  The output directory for debug is different than for release.

    Hope this helps,
    Michael Taylor — 11/23/05

I had one source code having 25 projects in single solution. It is consisting of both c++ as well as C# project. But now i wanted to call one of C# project(which is of windows application type) calls from a separate test project. I created a separate c# project and added the source reference, and created a object of source code class. it is building fine. but at the time of execution, it is throwing:

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

Please give me suggestion to resolve this issue.

Grant Winney's user avatar

Grant Winney

65k12 gold badges114 silver badges164 bronze badges

asked Mar 4, 2014 at 4:57

user2239053's user avatar

I got this execption and when I looked at the stack trace, it had another exeception: Arthimetic operation resulted in overflow. In my case, the application I was trying to run, required 32-bit mode where as the IIS in my Windows 8 machine runs in 64-bit mode by default. I had to set Enable 32-bit Applications to true for the AppPool this application was using.

The type initializer for ‘<Module>’ threw an exception and Arithmetic operation resulted in an overflow

Community's user avatar

answered Jan 6, 2015 at 23:44

DBK's user avatar

DBKDBK

4034 silver badges13 bronze badges

Experienced a similar problem when catching the error with a try-catch… Upon removing the try-catch block the full trace indicated FIPS as the culprit. Toggling the registry switch to 0 for FIPS solved the problem in this case.

answered Mar 22, 2017 at 8:10

Accentuate's user avatar

I have a c++/cli project and it’s a windows application. In debug mode we didn’t have any problems but after taking it to release mode this error start up. I searched and found some forum-answers but couldn’t help me solve this problem.
Please help me ….

Error :

An unhandled exception of type ‘System.TypeInitializationException’ occurred in Unknown Module.

Additional information: The type initializer for ‘Module’ threw an exception.

Updated 16-Nov-21 13:06pm


I had the same error and found that the class I was referencing was not the problem, but it had a static variable of another type declared that required another assembly to load. That assembly was built for a target platform of x86 (see Properties—>Build tab) however the main project was compiled for Any CPU. I rebuilt the older assembly for Any CPU… problem solved.

Here are some helpful steps for finding the root cause of this problem…

Click Debug—> Exceptions and check ON all the Thrown checkboxes. This will cause the debugger to stop on all first chance exceptions and will help you find the error under the Type Initializer error that you’re seeing. If it is related to another assembly, as mine was, you can use Microsoft’s Assembly Binding Log Viewer tool to help determine the problem.

Does the machine where the application is running has all the required dlls??

Maybe you are missing some interops in the local directory

I recently had the same problem, and it was because I was using Date.Parse on a missing element in the config file:

Date.Parse(System.Configuration.ConfigurationManager.AppSettings.Get(«frex»))

change your .net framework to lower ;)

In my case (so many different cases where this shows up) I was initializing some static fields in their declaration, like this:

public class MyHelperClass
{
    readonly static SolidColorBrush BlueBrush = new SolidColorBrush(Colors.Blue);
    
    public static void ApplyBrush()
    { 
      
    }
}

When I moved the initialization into a static constructor, the exception was no longer thrown:

public class MyHelperClass
{
  readonly static SolidColorBrush BlueBrush;
  
  static MyHelperClass
  {
    BlueBrush = new SolidColorBrush(Colors.Blue);
  }
}
look the repository container in unity.config
<pre><pre lang="xml">
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <alias type="App.Core.Interfaces.IRepositoryConfiguration, App.Core" alias="IRepositoryConfiguration" />
  <alias type="App.Repository.Mappings.SqlServer.SqlRepositoryConfigurer, App.Repository.Mappings.SqlServer" alias="SqlRepositoryConfigurer" />

  <container name="RepositoryContainer">
    <register type="IRepositoryConfiguration" mapTo="SqlRepositoryConfigurer" >
      <lifetime type="singleton"/>
    </register>
  </container>
</unity>
</pre></pre>

I was making an assignment in a constructor that didn’t have an error handling.

fileInfo = My.Computer.FileSystem.GetFileInfo(logDirLoc & logFileName)
writer = fileInfo.AppendText()

When I removed it the issue was solved.

tip: I looked through the error details and saw that a file couldn’t be found. That was it.

I faced the Same error that is «System.TypeInitializationException». This error is raised due to Static constructor .

Actually in my case i am accessing some value from the App.config and assigning that value in a static variable . The error is because of the App.config file . That means the app.config file is not well formed (I just missed some tag on app.config ). After putting the missing tag it works fine.

The type initializer for ‘Module’ threw an exception.
Just Run IISRESET

I had this problem also, but my solution wasn’t any of these. It was because I had an unmanaged class with a virtual function that returned a managed pointer.

class A
{
    virtual ManagedType^ Foo();
};

Apparently the C++/CLI runtime can’t handle this. Removing the virtual fixed the problem (luckily for me the virtual wasn’t needed in my case).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  • Remove From My Forums
  • Вопрос

  • Hi,

    I have an Azure solution with two roles, web and worker. After I got brave enough to start working with Azure Diagnostics. things went sour on me.

    The Web Role throws the following message : The type initializer for ‘<Module>’ threw an exception

    It seems to throw it when it hits this line :

                    String storageAccountString = RoleEnvironment.GetConfigurationSettingValue("DataStorage");
    

    I verified that the entry is specified and is correct, which should be as nothing has changed.

    Also through IntelliTrace I see «Could not load file or assembly ‘Microsoft.IdentityModel….», I verified that this assembly is in the published package.. so it is unclear to me why it cannot load it. However, when trying to debug using IntelliTrace it
    goes to the same RoleEnvironment line as shown above.

    Does anybody have an idea what the real reason is?

    Thanks,

    Wouter

Ответы

    • Помечено в качестве ответа

      8 марта 2011 г. 9:37

Понравилась статья? Поделить с друзьями:
  • Ошибка the system is low on memory
  • Ошибка there was an error loading script
  • Ошибка the system found unauthorized
  • Ошибка there was a problem with the network 400
  • Ошибка the system cannot find the path specified