J
jason
Hello everyone,
I have written a very simple windows service in .NET 2.0 C#. The
following is its OnStart() method:
protected override void OnStart(string[] args)
{
try
{
//Config.Load(AppDomain.CurrentDomain.BaseDirectory +
"TaskCenter.config");
Worker = new Thread(new ThreadStart(Work));
Worker.SetApartmentState(ApartmentState.STA);
Worker.Start();
}
catch (Exception ex)
{
Error.ProcessServiceError(ex, MachineName,
"192.168.16.2", "Local");
}
}
The Worker thread is also incredibly simple:
private void Work()
{
while (true)
{
Thread.Sleep(1000);
try
{
DataTable dt = Task.GetToDoList();
Task t = new Task();
foreach (DataRow dr in dt.Rows)
{
t.Populate((Guid)dr["taskid"]);
t.Execute();
}
}
catch (Exception ex)
{
Error.ProcessServiceError(ex, MachineName,
"192.168.16.2", "Local");
}
}
}
Error and Task both come from a class library (also .NET 2.0 in C#).
When I originally wrote this service, I stubbed out the
Task.GetToDoList method to just write to a log file. In that stubbed
out world, everything worked correctly. I could install the service
using InstallUtil.exe, I could start the service in the control panel,
I could attach to the service using the VS debugger, and watch it step
through the code with the predicted results.
The problem started when I tried to replace the stub with real code.
The real version of the GetToDoList method needs to talk to the
database. The class library includes a reusable data utility class
that expects the consuming application to have a configuration
settings file, and gets the configuration strings from the
ConfigurationManager class.
No problems there, the class library gets that information just fine
when used by the ASP.NET website and the web service which were also
written to use the class library.
The problem is only with the windows service configuration. I added an
app.config file to the project through the VS2005 menu system (Add ->
New item -> Application Configuration). The application configuration
file started off nearly empty, and I only modified it to include the
necessary connection string:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add name="Database"
connectionString="Server=12.3.4.56;Database=mydb;uid=user;pwd=password;"/</configuration>
Suddenly, after adding this file, the windows service won't start.
Installation still succeeds with InstallUtil, but when I attempt to
start the service I get "Error 1053" stating that the service did not
respond to the start request in a timely manner. From everything I've
read on the net, this is often caused by an unhandled exception in
OnStart(). I don't think that's the case this time though, since the
application works just fine without the configuration file.
To further get evidence to this end, I went to the Debug folder for
the windows service and renamed the MyService.exe.config file to
M.E.C. Suddenly the service starts just fine (didn't even need to
reinstall it). However when I rename the file back, the service fails
to start again.
I am assuming that the problem is in the app.config file itself, and
that the service is choking when it tries to read the document in at
start-time. Anyone have any ideas? Something obviously missing from
the config file maybe?
Thanks for any help,
Jason
I have written a very simple windows service in .NET 2.0 C#. The
following is its OnStart() method:
protected override void OnStart(string[] args)
{
try
{
//Config.Load(AppDomain.CurrentDomain.BaseDirectory +
"TaskCenter.config");
Worker = new Thread(new ThreadStart(Work));
Worker.SetApartmentState(ApartmentState.STA);
Worker.Start();
}
catch (Exception ex)
{
Error.ProcessServiceError(ex, MachineName,
"192.168.16.2", "Local");
}
}
The Worker thread is also incredibly simple:
private void Work()
{
while (true)
{
Thread.Sleep(1000);
try
{
DataTable dt = Task.GetToDoList();
Task t = new Task();
foreach (DataRow dr in dt.Rows)
{
t.Populate((Guid)dr["taskid"]);
t.Execute();
}
}
catch (Exception ex)
{
Error.ProcessServiceError(ex, MachineName,
"192.168.16.2", "Local");
}
}
}
Error and Task both come from a class library (also .NET 2.0 in C#).
When I originally wrote this service, I stubbed out the
Task.GetToDoList method to just write to a log file. In that stubbed
out world, everything worked correctly. I could install the service
using InstallUtil.exe, I could start the service in the control panel,
I could attach to the service using the VS debugger, and watch it step
through the code with the predicted results.
The problem started when I tried to replace the stub with real code.
The real version of the GetToDoList method needs to talk to the
database. The class library includes a reusable data utility class
that expects the consuming application to have a configuration
settings file, and gets the configuration strings from the
ConfigurationManager class.
No problems there, the class library gets that information just fine
when used by the ASP.NET website and the web service which were also
written to use the class library.
The problem is only with the windows service configuration. I added an
app.config file to the project through the VS2005 menu system (Add ->
New item -> Application Configuration). The application configuration
file started off nearly empty, and I only modified it to include the
necessary connection string:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add name="Database"
connectionString="Server=12.3.4.56;Database=mydb;uid=user;pwd=password;"/</configuration>
Suddenly, after adding this file, the windows service won't start.
Installation still succeeds with InstallUtil, but when I attempt to
start the service I get "Error 1053" stating that the service did not
respond to the start request in a timely manner. From everything I've
read on the net, this is often caused by an unhandled exception in
OnStart(). I don't think that's the case this time though, since the
application works just fine without the configuration file.
To further get evidence to this end, I went to the Debug folder for
the windows service and renamed the MyService.exe.config file to
M.E.C. Suddenly the service starts just fine (didn't even need to
reinstall it). However when I rename the file back, the service fails
to start again.
I am assuming that the problem is in the app.config file itself, and
that the service is choking when it tries to read the document in at
start-time. Anyone have any ideas? Something obviously missing from
the config file maybe?
Thanks for any help,
Jason