Windows Service - Reporting Error at Start

J

Joe

C# .NET 2.0

I've got a Windows Service that works exactly as I expect it to. However,
it does rely on a few Configuration settings. If the customer doesn't
configure it correctly, the service shouldn't start.

So I've put in some try...catchs, but what I'm not sure how to do is not
allow the service to start. If attempted to start with the NET START
command, throwing an exception keeps the service from starting, but the
message reported back is :

The MyService service is starting.
The MyService service could not be started.

The service did not report an error.

So how do I report this error? Writing it to the EvenLog doesn't resolve
it, although there is an entry in there as expected.

Secondly, if the service is started via the SMC, the service doesn't start
as expected, but I'm given the following error:

The MyService 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.

So how do I prevent the service from starting (although in both these
cases, the service doesn't actually start) and make whatever appropriate
error reports necessary?

Thanx!
 
G

Guest

I've got a Windows Service that works exactly as I expect it to. However,
it does rely on a few Configuration settings. If the customer doesn't
configure it correctly, the service shouldn't start.

Perhaps, in OnStart you just don't call the base class' OnStart if the
criteria aren't met?

That ought to keep it from starting.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

IIRC if you throw an exception in the onstart and do not catch it the
service does not start and you get an error msg.

Additionally I would insert a event log with a detailed description of what
happened
 
J

Joe

I do have a try...catch (was pretty sure my original message stated as
much). And there are EventLog entries, but it still doesn't perform as
expected...
 
J

Joe

I found my own answer, and it was something I overlooked. The ServiceBase
class has an ExitCode property. Setting this, and still re-throwing the
exception in the OnStart does what I expect and want it to do.

Thanx!
 
T

tdavisjr

Just curious. I have looked everywhere in VS 2005 for the Windows
Service Project Template. I can't find it anywhere. Were you able to
find it?
 
J

Joe

if you mean the project wizard within the IDE itself, then yes... if you
mean the physical files that actually make up the template, then no...
 

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