Click here to Skip to main content
14,365,441 members
Rate this:
Please Sign up or sign in to vote.
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 9-Nov-19 3:10am
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);
}

}

}
Rate this:
Please Sign up or sign in to vote.

Solution 5

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.
   
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!
Brett Nemec 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
Emmablakes 30-Jul-16 11:18am
   
thanks bro..reading your solution actually helped me too.
good to have people like you around
Member 14605063 17-Oct-19 16:13pm
   
Worked for me! Got right to the heart of the problem.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

Maybe you are missing some interops in the local directory
   
Comments
Prasad_Kulkarni 5-Jun-12 1:24am
   
Good one +5
Rate this:
Please Sign up or sign in to vote.

Solution 3

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"))
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

change your .net framework to lower ;)
   
Comments
Abdelrahman Othman Helal 24-Apr-17 6:43am
   
Thank you, You saved my whole day :).
Rate this:
Please Sign up or sign in to vote.

Solution 11

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()
    { 
      // do something with brush 
    }
}


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);
  }
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 7

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>
   
Rate this:
Please Sign up or sign in to vote.

Solution 9

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 10

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.
   
Comments
Bimaln 24-Sep-14 9:53am
   
Actually it was fixed my error.
Rate this:
Please Sign up or sign in to vote.

Solution 13

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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100