Initialising a Windows service

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

I'm looking for advice concerning what to do if a Windows service does not
find a "viable" working environment at startup.

E.g. I have a Windows service which does the following:

1) connects to a remote FTP site and looks for certain files which it
downloads to a fileserver if it finds them;

2) processes the files and writes their contents into various tables in
various databases on various servers;

3) generates a mail message containing various statistics to various
Exchange and POP3 mailboxes.

When it starts up, it calls a function which

1) attempts a logon to the remote FTP site;

2) tries to create (and then delete) an empty text file on the file server;

3) sends a test email to the network manager.

If any one of the above processes fails on startup, the service considers
that it doesn't have a viable working environment in which to run, so
disables the System.Timer object.

I've now been asked that this should actually stop the service altogether,
because the network has service monitoring software which would detect this
and send alerts etc.

Would you consider that this was a bit too drastic, and that the service
should continue to try to find a viable working environment, at least for a
specified number of times?

If not, what is the safest / cleanest way for a Windows service to shut
itself down?

Any asssistance gratefully received.

Mark
 
I asked something similiar not too long ago, and the answer I got is in your
service OnStart method, if the environemnt isn't viable, throw an exception.
This is supposed to prevent the service from even starting... I haven't had
an opportunity to try it out yet, hopefully I can get back to that project
this week sometime...
 
I asked something similiar not too long ago, and the answer I got is in
your service OnStart method, if the environemnt isn't viable, throw an
exception. This is supposed to prevent the service from even starting... I
haven't had an opportunity to try it out yet, hopefully I can get back to
that project this week sometime...

I'll check it out - thanks for the reply.
 
JSheble said:
Let me know how it goes...

Absolutely throwing an exception will stop the service from starting. I've
seen this many times with services I've written... not that they were
supposed to thrown exceptions, but.
 
I asked something similiar not too long ago, and the answer I got is
in your service OnStart method, if the environemnt isn't viable, throw
an exception. This is supposed to prevent the service from even
starting... I haven't had an opportunity to try it out yet, hopefully
I can get back to that project this week sometime...

Unless you (a) can determine the viability of the environment fairly
quickly, and (b) want windows to report that the service failed to start, I
wouldn't use an exception in OnStart(...) because if the OnStart(...) takes
too long then Windows may report that the service didn't start in the
expected amount of time (its like 30 seconds or something I'm not sure.)
Usually, this is not desirable.

Instead, you should have the OnStart(...) start up a new thread that tries
to establish the environment. If it is successful, then continue
processing as you would otherwise. If it is NOT successful, and you want
the service to stop (without an error), then you can use the
ServiceController class to issue a stop command to yourself.
 

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

Back
Top