The type initializer for 'MyClass' threw an exception
The following is my Windows service code. When I am debugging the code, I am getting the error/ exception:
The type initializer for 'CSMessageUtility.CSDetails' threw an exception.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using CSMessageUtility;
namespace CS_Data_Trasmmiting_Service
{
public partial class svcCSWinServ : ServiceBase
{
//private string sLogFormat;
//private string sErrorTime;
private Thread new_thread;
Logger logObject = new Logger();
private bool isenable = true;
public svcCSWinServ()
{
InitializeComponent();
logObject.append("Initialize Service " + DateTime.Now.ToString(), 70);
CheckForAlarms();
}
protected override void OnStart(string[] args)
{
try
{
new_thread = new Thread(new ThreadStart(CheckForAlarms));
new_thread.Start();
}
catch
{
}
logObject.append("Service Started successfully " + DateTime.Now.ToString(), 70);
}
protected override void OnStop()
{
try
{
isenable = false;
new_thread.Abort();
}
catch
{
}
logObject.append("Service Stopped successfully " + DateTime.Now.ToString(), 70);
}
void CheckForAlarms()
{
try
{
while (true)
{
//if((DateTime.Now.ToString("HH:mm") == "18:00"))
//{
logObject.append("Start Sending Data " +DateTime.Now.ToString(), 70);
try
{
//SendAllInfo();
string str = CSMessageUtility.CSDetails.createDHSMessageFormat();
Thread.Sleep(2000);
string str1 = CSMessageUtility.CSDetails.createEALMessageFormat();
Thread.Sleep(2000);
string str2 = CSMessageUtility.CSDetails.createProductStatusMessageForamt();
Thread.Sleep(2000);
string str3 = CSMessageUtility.CSDetails.createEODMessageFormat();
Thread.Sleep(2000);
string str4 = CSDetails.createProductReceiptEntryatBOSMessageFormat();
Thread.Sleep(2000);
string str5 = CSMessageUtility.CSDetails.createProductSaleMessageFormat();
Thread.Sleep(2000);
string str6 = CSMessageUtility.CSDetails.createTotalizerExceptionMessageFormat();
Thread.Sleep(2000);
//CSMessageUtility.CSDetails.createDailyCOtransferMessageFormat();
//Thread.Sleep(2000);
}
catch (Exception ee)
{
logObject.append(ee.Message, 70);
}
logObject.append("Finished Sending Data " +DateTime.Now.ToString(), 70);
Thread.Sleep(3000);
//}
//Thread.Sleep(20000);
}
}
catch (Exception ex)
{
logObject.append("Thread Exception: "+ ex.Message + " "+ DateTime.Now.ToString(), 70);
try
{
new_thread.Abort();
}
catch (Exception ex1)
{
logObject.append("Thread Exception: " +ex1.Message + " " + DateTime.Now.ToString(), 70);
}
if (isenable == true)
{
new_thread = new Thread(new ThreadStart(CheckForAlarms));
new_thread.Start();
}
}
}
}
}
Check the InnerException
property of the TypeInitializationException
; it is likely to contain information about the underlying problem, and exactly where it occurred.
This problem can be caused if a class tries to get value of a key in web.config or app.config which is not present there.
e.g.
The class has a static variable
private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();
But the web.config doesn't contain the GoogleCalendarApplicationClientID
key
The error will be thrown on any static function call or any class instance creation
The type initializer for 'CSMessageUtility.CSDetails' threw an exception.
means that the static constructor on that class threw an Exception - so you need to look either in the static constructor of the CSDetails class, or in the initialisation of any static members of that class.
I ran into the same problem, when I was using a static methods in a Util class, just like you had used CSMessageUtility.CSDetails
.
The problem was that during the static initialization of the class (using the static constructor), the framework also initialize the the static variables (fields) in the class. I had a static variable that attempts to read values from app.config
, and app.config
was missing the respective settings, thus resulting in an un-handled exception. This resulted in getting the
"Object reference not set to an instance of an object."
as the inner exception.