470,572 Members | 2,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

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

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()

public void init()
string xmlfile = AppDomain.CurrentDomain.BaseDirectory +
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
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
myHandler = new DBHandler(connectionstring);
myLogger.LogEvent("Inside mainprocess.init(). DBHandler
catch(Exception x)
myLogger.LogEvent("Error on init: " + x.Message);

Further, here is the constructor for DBHandler:
public DBHandler(string connection)
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. " +
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.
Jon Gazsi
Nov 16 '05 #1
0 1824

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jinsu Park | last post: by
reply views Thread by Softer | last post: by
4 posts views Thread by MSNews | last post: by
2 posts views Thread by RvGrah | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.