C'est ce que je suis:Quelle est la bonne façon de quitter Windows Service OnStart si la configuration est incorrecte et que rien à faire?
protected override void OnStart(string[] args)
{
if (SomeApp.Initialize())
{
SomeApp.StartMonitorAndWork();
base.OnStart(args);
}
}
protected override void OnStop()
{
SomeApp.TearDown();
base.OnStop();
}
Ici Initialiser lit un fichier de configuration et si elle est mal il n'y a rien à faire le service devrait cesser! Si la configuration est correcte, StartMonitorAndWork démarre:
Timer(new TimerCallback(DoWork), null, startTime, loopTime);
et DoWork interroge périodiquement la base de données.
Si Initialiser échoue (je vérifie le fichier journal) et je tente d'arrêter le service de Outils- administratif> Services que je reçois:
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?
ou devrait-il y avoir quelque chose comme ceci:
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();
}
}
Merci & BR - Matti
EDIT: Je suis venu avec quelque chose comme ceci:
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();
}
}
Il y a une meilleure réponse de l'OMI - [? Quelle est la bonne façon pour un service Windows à l'échec] (http://stackoverflow.com/a/4197271/298054). – jweyrich