Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.58/5 (5 votes)
See more:
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.
Posted
Updated 16-Nov-21 13:06pm
v2
Comments
sharifani 1-Nov-12 11:40am    
who can i resolve error " the type initializer 'MODUl' threw an exception.i can install microsoft visual studio 2010 ultimate,but that error ocured and vs not running.please help me.
Member 14504152 18-Jun-19 9:57am    
I have also this type of error in my code

using Nexmo.Api;
using System.Web.Mvc;
using System.Diagnostics;
using System.Web.Http;
using Newtonsoft.Json;
using System.IO;

namespace NexmoDotNetQuickStarts.Controllers
{
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

public class SMSController : Controller
{

public ActionResult Index()
{
return View();
}

[System.Web.Mvc.HttpGet]
public ActionResult Send()
{
return View();
}

[System.Web.Mvc.HttpPost]
public ActionResult Send(string to, string text)
{

var results = SMS.Send(new SMS.SMSRequest
{

from = Configuration.Instance.Settings["appsettings:NEXMO_FROM_NUMBER"], //in this line
to = to,
text = text
});
return View("Index");
}

[System.Web.Mvc.HttpGet]
public ActionResult Recieve([FromUri]SMS.SMSInbound response)
{

if (null != response.to && null != response.msisdn)
{
Debug.WriteLine("-------------------------------------------------------------------------");
Debug.WriteLine("INCOMING TEXT");
Debug.WriteLine("From: " + response.msisdn);
Debug.WriteLine(" Message: " + response.text);
Debug.WriteLine("-------------------------------------------------------------------------");
return new HttpStatusCodeResult(200);

}
else
{
Debug.WriteLine("-------------------------------------------------------------------------");
Debug.WriteLine("Endpoint was hit.");
Debug.WriteLine("-------------------------------------------------------------------------");
return new HttpStatusCodeResult(200);

}

}

[System.Web.Mvc.HttpGet]
public ActionResult DLR([FromUri]SMS.SMSDeliveryReceipt response)
{

Debug.WriteLine("-------------------------------------------------------------------------");
Debug.WriteLine("DELIVERY RECIEPT");
Debug.WriteLine("Message ID: " + response.messageId);
Debug.WriteLine("From: " + response.msisdn);
Debug.WriteLine("To: " + response.to);
Debug.WriteLine("Status: " + response.status);
Debug.WriteLine("-------------------------------------------------------------------------");

return new HttpStatusCodeResult(200);
}

}

}

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.
 
Share this answer
 
Comments
benben88 20-Jun-14 12:12pm    
This helped me! Initializing all of the "thrown" actually gives you a better insight as to what is happening to get this bigger error. Many thanks!
thenem 12-Aug-14 13:34pm    
Thanks! Ticking all of the "thrown" checkboxes under Debug --> Exceptions was a great help!
Member 10273358 8-Dec-14 20:49pm    
Very good solution. It fast helped me find the error that I declared a const std::set with incorrect initialization arguments. It occurs before main() entry, so without this, it's really hard to find it out!
hamidsadegh 31-Dec-15 13:57pm    
thanks for your answer
pratibhas 3-Mar-16 7:14am    
thanx
Does the machine where the application is running has all the required dlls??

Maybe you are missing some interops in the local directory
 
Share this answer
 
Comments
Prasad_Kulkarni 5-Jun-12 1:24am    
Good one +5
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"))
 
Share this answer
 
Comments
Farhat Khan 18-Apr-23 0:01am    
public partial class MDI : Form
{
public MDI()
{
InitializeComponent();
}

private void eXITToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void MDI_Load(object sender, EventArgs e)
{
string Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if(File.Exists(Path+"\\connect"))
{
login log = new login();
MainClass.showWindow(log, this);
}
else
{
settings set = new settings();
MainClass.showWindow(set, this);
}
}
please solution
An unhandled exception of type 'System.TypeInitializationException' occurred in ims.exe

Additional information: The type initializer for 'ims.MainClass' threw an exception.
run and this error accur
change your .net framework to lower ;)
 
Share this answer
 
Comments
Abdelrahman Othman Helal 24-Apr-17 6:43am    
Thank you, You saved my whole day :).
In my case (so many different cases where this shows up) I was initializing some static fields in their declaration, like this:

C#
public class MyHelperClass
{
    readonly static SolidColorBrush BlueBrush = new SolidColorBrush(Colors.Blue);
    
    public static void ApplyBrush()
    { 
      // do something with brush 
    }
}


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

C#
public class MyHelperClass
{
  readonly static SolidColorBrush BlueBrush;
  
  static MyHelperClass
  {
    BlueBrush = new SolidColorBrush(Colors.Blue);
  }
}
 
Share this answer
 
XML
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>
 
Share this answer
 
I was making an assignment in a constructor that didn't have an error handling.

VB
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Bimaln 24-Sep-14 9:53am    
Actually it was fixed my error.
The type initializer for 'Module' threw an exception.
Just Run IISRESET
 
Share this answer
 
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).
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900