Error in Windows Service: The type initializer for "..." threw an exception

J

Jonathan G.

I'm writing my first windows service. I have a class called
'mainprocess' whose constructor is called by the service class.
Within mainprocess, I declare an instance of another class I wrote,
called DBHandler, which does all of my Sql Database work.

I have LogEvents everywhere and notice that the thread never gets
inside the DBHandler constructor. Instead, it throws this error as a
new instance of the DBHandler class is declared: 'Error in Windows
Service: The type initializer for "DBHandler" threw an exception'.

Here's where it's called in mainprocess:

public mainprocess()
{
init();
}

public void init()
{
try
{
string xmlfile = AppDomain.CurrentDomain.BaseDirectory +
"\\settings.xml";
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(xmlfile);
XmlElement element = xml.DocumentElement;
XmlNode node = element.SelectSingleNode("dbconnection[1]");
connectionstring = node.InnerText;
node = element.SelectSingleNode("picdirpath[1]");
picdir = node.InnerText;
node = element.SelectSingleNode("testbmp[1]");
testbmp = node.InnerText;
myLogger.LogEvent("Inside mainprocess.init(). DBHandler not yet
initialized.");
//WE CRASH AT THE NEXT LINE OF CODE!!!
myHandler = new DBHandler(connectionstring);
myLogger.LogEvent("Inside mainprocess.init(). DBHandler
initialized");
}
catch(Exception x)
{
myLogger.LogEvent("Error on init: " + x.Message);
}
}



Further, here is the constructor for DBHandler:


public DBHandler(string connection)
{
try
{
connectionstring = connection;
myLogger.LogEvent("Inside DBHandler. connection=" + connection);
myConnection.ConnectionString = connectionstring;
myLogger.LogEvent("Inside DBHandler.
myConnection.ConnectionString set");
}
catch(Exception e)
{
myLogger.LogEvent("Error in DBHandler constructor. " +
e.Message);
}
}


I know none of the constructor is being executed because nothing is
written to the event log.

The funny thing is, when I take the exact same code and wire it up to
a button click in an asp.net web app, it works fine. It just doesn't
work as a windows service. I'm at a loss.
Thanks,
Jon Gazsi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top