Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to exit Windows Service OnStart if configuration is wrong and nothing to do?

This is what I got:

protected override void OnStart(string[] args) {     if (SomeApp.Initialize())     {         SomeApp.StartMonitorAndWork();         base.OnStart(args);     } }  protected override void OnStop() {     SomeApp.TearDown();     base.OnStop(); } 

Here Initialize reads a config file and if it's wrong there's nothing to do so service should STOP! If config is ok StartMonitorAndWork starts:

Timer(new TimerCallback(DoWork), null, startTime, loopTime); 

and DoWork polls database periodically.

If Initialize fails (i check log file) and I try to stop service from Administrative Tools->Services i get:

 Could not stop the SomeService on Local Computer. The service did not return an error.  This could be internal Windows error or an internal service error.  If the problem persists, contact system administrator. 
 The question is:  "Is exiting OnStart without doing nothing enough if Initialize returns false? 

OR should there be something like this:

private void ExitService() {     this.OnStop();     System.Environment.Exit(1); }  protected override void OnStart(string[] args) {     if (ObjectFolderApp.Initialize())     {         SomeApp.StartMonitorAndWork();         base.OnStart(args);     }     else     {         ExitService();     } } 

Thanks & BR - Matti

EDIT: I came up with something like this:

protected override void OnStart(string[] args) {     try     {         if (SomeApp.Initialize())         {             SomeApp.StartMonitorAndWork();             base.OnStart(args);         }         else         {             Stop();         }     }     catch     {         Stop();     } }  protected override void OnStop() {     try     {         SomeApp.TearDown();         base.OnStop();     }     catch     {         base.OnStop();     } } 
like image 519
char m Avatar asked Apr 21 '10 07:04

char m


People also ask

What is Windows service in C#?

A Windows service is a long-running application that can be started automatically when your system is started. You can pause your service and resume or even restart it if need be. Once you have created a Windows service, you can install it in your system using the InstallUtil.exe command line utility.


2 Answers

after testing all the approaches I personally prefer calling

Environment.FailFast("Configuration is wrong."); 

Main goal is that the fail explanation is written in Event Log and FailFast affects on Recovery settings of your service. So if you configure recovery and configuration gets correct your service will start automatically.

like image 62
Nisus Avatar answered Oct 08 '22 23:10

Nisus


I know it's not pretty but throwing an exception in OnStart also works.

If your service is setup to "Autolog" this will also write a your exception message to the EventLog automatically.

protected override void OnStart(string[] args) {     if (ObjectFolderApp.Initialize())     {         SomeApp.StartMonitorAndWork();         base.OnStart(args);     }     else     {         throw new Exception("What went wrong");     } } 
like image 30
ParmesanCodice Avatar answered Oct 08 '22 22:10

ParmesanCodice