views:

137

answers:

3

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();
    }
}
+2  A: 

I would log an error to the eventlog if Initialize() returns false with some sensible message saying while it fails, and you should like you suggest call OnStop() if it fails. It's good practice to ensure proper shutdown of the service.

Also see this related SO question, and dev newsgroup thread.

Mikael Svenson
thanks, sorry but i didn't get it. can you explain more: "eturns false with some sensible message saying while it fails, and you should like you suggest call OnStop()"
matti
ok. now i get some of it so if Initialize returns false I should call OnStop. what about System.Environment.Exit(1);??
matti
Calling Enviroment.Exit() after the OnStop is perfectly fine. This will send an error to your service. If it's set to retry then sending an Exit message will allow it to retry. Exit is also preferred over FailFast unless you have corrupted state in your application which cannot repair.
Mikael Svenson
A: 

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.

Nisus
actually some people suggest to call Stop. this gets OnStop to be called.
matti
+1  A: 

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");
    }
}
ParmesanCodice