views:

133

answers:

5

I developed a simple windows service in C# as per this article.

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

I was successfully able to start the service for the first time and stop it. During the following attempts, I was not able to start the service. I got the following information.

The MyNewService service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.

Please help.

A: 

Did you look in the event log? You can usually get more detailed error information there about the service error. Also, are you writing out to a log with your service? That's another way you could figure out what's going wrong.

You can get to the event log by right clicking on Computer and selecting "Manage". Under System Tools, look under Event Viewer->Application. This is on Windows XP, but other Windows OS's should be similar.

dcp
+1  A: 

Basically this means the main thread of your service has crashed for some reason. The most common I've seen is filesystem access to it's own log files.

Sometimes you can find the reason in the event viewer, but unfortunately a lot of the time the user you're running the service as won't actually have access to log it's error. A simple thing to do if you're in a dev environment is to just give the service an administrator account temporarily, firstly cause it'll tell you whether the crash is being caused by lack of access (cause it'll work) and secondly if it's not it'll allow it to write to the event viewer. Make sure to take the admin access of f once you fix it though, cause long-term that can be very dangerous.

Tim Schneider
Actually the service is being run with local system account privilege. It actually started and stopped the first time I tried. And also it logged events. But when I try starting it for the second time, it did not!
bdhar
A: 

If the service is on your development machine, you should be able get Visual Studio's debugger to attach to it as it starts so that you could identify if anything is causing it to crash. It involves a bit of registry editing as described here: http://blogs.msdn.com/greggm/archive/2005/02/21/377663.aspx

Jason Musgrove
+1  A: 

I outlined here a method we're using to debug our Windows services. Maybe this will help you trace the error. Basically this sounds like some error is occurring while trying to execute the OnStart method.

Thorsten Dittmar
Thanks all. There was a bug in the OnStart method!
bdhar
A: 

It sounds like your main thread is dying for some reason. Put a call to System.Diagnostics.Debugger.Break() in your service's startup code, e.g., the Main entry point, the service constructor, or the OnStart() method. When you start your service from the Services MMC, you'll be prompted to enter a debug session. Once you're in Visual Studio, open the Exceptions dialog (from the Debug menu) and check the boxes in the Thrown column. Then debug from there to find the problem.

Matt Davis