Canceling Windows Service Start

  • Thread starter Thread starter JSheble
  • Start date Start date
J

JSheble

I've written a Windows Service using C#, but I cannot seem to figure out how
to cancel or prevent the service from starting under certain conditions.
For example, in the OnStart I read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc) I don't want the servie to start. So how would I
cancel the start up?
 
Hi,
I dont have a very good knowledge on this. But from what I know u cannot
start or stop a servce within a service. This has to be done by a writing a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc)
The above portion has to be done by the Controller and then the service has
to be started.
PLease read more about monitoring windows services in MSDN
 
I am not talking about a monitoring app, I'm talking I don't want the
service itself to start unless certain conditions are met. If they try to
start the service, and those conditions fail, then the servie should not
start. How does the service not allow itself to be started?
 
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing an
exception? Is there a certain type of exception I should throw?
 
JSheble said:
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing an
exception? Is there a certain type of exception I should throw?

protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 
That's exactly what I have in the code, but the service still shows as
running in the SMC...

Willy Denoyette said:
JSheble said:
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing
an exception? Is there a certain type of exception I should throw?

protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 
Should work for you as well, try to remove all code there is in OnStart and
leave the throw clause. If this doesn't work either post your code
(OnStart).

Willy.

JSheble said:
That's exactly what I have in the code, but the service still shows as
running in the SMC...

Willy Denoyette said:
JSheble said:
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing
an exception? Is there a certain type of exception I should throw?

protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 
And make sure you refresh the service status in the SMC....

Willy.

JSheble said:
That's exactly what I have in the code, but the service still shows as
running in the SMC...

Willy Denoyette said:
JSheble said:
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing
an exception? Is there a certain type of exception I should throw?

protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 
Well I did exactly what you said, commenting out all the code in my OnStart,
threw an exception, looked in the SMC and it still shows as running... I can
even press the start and stop buttons in the SMC, and it stops and starts
with no problem...

Here's the OnStart code... as I said, it does nothing but throw the
exception:

protected override void OnStart(string[] args)
{
throw(new Exception("Service failed to start"));

// this.oAppSettings.ListenPort =
Convert.ToInt32(ConfigurationSettings.AppSettings["ListenPort"].ToString());
// this.oAppSettings.LogPath =
ConfigurationSettings.AppSettings["LogPath"].ToString();
// this.oAppSettings.TestPath =
ConfigurationSettings.AppSettings["TestPath"].ToString();
// this.oAppSettings.ConnectionTimeout =
Convert.ToInt32(ConfigurationSettings.AppSettings["ConnectionTimeout"].ToString());
// this.oAppSettings.DataDSN =
ConfigurationSettings.AppSettings["DataDSN"].ToString();
// this.oAppSettings.ShowGreeting =
Convert.ToBoolean(ConfigurationSettings.AppSettings["ShowGreeting"].ToString());
// this.oAppSettings.GlobalParse =
Convert.ToBoolean(ConfigurationSettings.AppSettings["GlobalParse"].ToString());
// this.oAppSettings.OnCustomParse =
ConfigurationSettings.AppSettings["OnCustomParse"].ToString();
// this.oAppSettings.OnResponse =
ConfigurationSettings.AppSettings["OnResponse"].ToString();
//
// if((this.oAppSettings.LogPath != "") &&
!this.oAppSettings.LogPath.EndsWith("\\"))
// {
// this.oAppSettings.LogPath += "\\";
// }
//
// if((this.oAppSettings.TestPath != "") &&
!this.oAppSettings.TestPath.EndsWith("\\"))
// {
// this.oAppSettings.TestPath += "\\";
// }
//
// if(this.oAppSettings.ConnectionTimeout > 0)
// {
// this.oAppSettings.ConnectionTimeout *= (1000 * 60);
// }
//
// // TODO add appropriate validation for config settings
// // TODO throwing an exception here should cause the OnStart to fail...
// // throw(new Exception("Service failed to start... config errors"));
//
// WMI_StatusProvider.FireEvent("GSRouter Service Started", "GSRouter",
EventType.ServiceStart);
// this.sockServer.Listen(this.oAppSettings.ListenPort);
// this.dtUpTime = DateTime.Now;
}


Willy Denoyette said:
Should work for you as well, try to remove all code there is in OnStart
and leave the throw clause. If this doesn't work either post your code
(OnStart).

Willy.

JSheble said:
That's exactly what I have in the code, but the service still shows as
running in the SMC...

Willy Denoyette said:
Is there anything special I should know about this? I tried throwing
an exception in my OnStart, but the service still shows as running in
the SMC... Is there something else that has to occur besides just
throwing an exception? Is there a certain type of exception I should
throw?



protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 
Please accept my most humblest aplogies... it turns out I had changed the
name of my servie exe file, but didn't change the batch file that installs
and starts the service... so the service that I was actually testing against
wasn't the service that had the exception code in it...

Again, my most humble apology...

Willy Denoyette said:
Should work for you as well, try to remove all code there is in OnStart
and leave the throw clause. If this doesn't work either post your code
(OnStart).

Willy.

JSheble said:
That's exactly what I have in the code, but the service still shows as
running in the SMC...

Willy Denoyette said:
Is there anything special I should know about this? I tried throwing
an exception in my OnStart, but the service still shows as running in
the SMC... Is there something else that has to occur besides just
throwing an exception? Is there a certain type of exception I should
throw?



protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.
 

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