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.
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.
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"))
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
>
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.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
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);
}
}
}