Another Windows Service Question

J

Jerry Camel

I want my service to terminate automatically if the specified parameters are
invalid. I tried to use a servicecontroller component to attach to the
service, but I think that it's failing because it's being called in the
OnStart event (where I validate the parameters) and the service isn't fully
running yet. How can I stop my service from within the service itself?
Thanks.

Jerry
 
J

John Timney \(Microsoft MVP\)

why not let your service start cleanly, and then terminate it in your first
thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
J

Jerry Camel

That's a great idea, but how do you terminate the thread from within? I
tried setting a flag and terminating in the timer event, but the service
never stopped. What's the code for stopping the service? And how long
after the OnStart event do you have to wait before you can terminate
cleanly?

This is what I tried (from memory, so this might not be perfect syntax):

...Sub TimerElapseEvent...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.Stop
Else
...
End If
End Sub

Is there a more appropriate way to stop the service?

Thanks for your help.

Jerry
 
J

Jerry Camel

I replied to this yesterday, but apparently the post never made it...

Thanks for the response, John. I've tried just what you're suggesting.
Even if I do it after the OnStart event, what's the proper way to terminate
the service from within itself? Do you still have to use a
ServiceController object? In the timer's elapsed event, I tried somethign
like this, but it doesn't seem to do anything:

Sub...Elapsed Event...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.stop
Else
....
End If
End Sub

Is there a more appropriate way to stop the service? How soon after the
OnStart event finishes can you do this? Thanks.

Jerry
 
J

John Timney \(Microsoft MVP\)

Hi Jerry,

Well the approach I would suggest is unfortunately what your doing - you do
need a serviceController instance to stop it via code. The code I would use
is identical to your VB code (albeit c#).

ServiceController serviceMonitor = new ServiceController("servicename");
if( serviceMonitor == ServiceControllerStatus.Running )
{
serviceMonitor.Stop();
}

The only difference I can see would be that I would typically perform a
check to see if the service was actually running before trying to termnate
in case it had not actually started. You cant stop a service thats not
completed its onStart event, timer elapsed should not kick in until until
onStart completes.

Are you trying to stop the service, or actually terminate it, putting a
service into a stopped state is "usually" very straightforward and the code
you have should suffice - you cant easily however terminate a service
without being a bit of a hacker and actually starting it with process start
and killing the process ID.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
J

Jerry Camel

I just want to stop the service. At tehm oment, I'm just using and End
command which does the trick albeit without grace.
I'll play with it a bit, but I'm pretty sure that I'm not getting to my
Elapsed event until after the service is officially started. (I'm getting
the auto-logged "Started" event and timer.start is the last line of the
OnStart handler.) Besides, I even tried attaching to another running
service - one that'd been running since system start and I get the same
exception. Problem is that the exception doesn't say why it can't attach,
just that it can't...

But! As I'm typing this I just had an epif - ipiph -eepiph... I had an
idea! My test box is a Win2K3 server... And I was usign the new
NetworkService account... I'll be that it's not allowed to attach to a
service. What do you think? Well, something else to test, anyway...

Is there a way to specify that an account has the right to start and stop
specific services? Thanks for your insight...

Jerry
 
J

John Timney \(Microsoft MVP\)

Is there a way to specify that an account has the right to start and stop
specific services? Thanks for your insight...


Local security policy under user rights allows you to specify who can log on
as a service..............Why dont you just set your service to a really
high privelaged user and see if your error goes away.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
J

Jerry Camel

For testing that would work, but I'm not sure the high mucky-mucks would
want a priveledged account assinged to a specific service. If you could
limit the priveledge to the specific service, that would be ideal. I'll
play with it. My production server isn't going to be a Win2K3 box, so I'm
not going to be able to use that service account anyway.
 
J

John Timney \(Microsoft MVP\)

Then create an account that can log on as a service and use that named
account only for this particular service. It probably doesn't need to have
much in the way or authoritive rights - ie it doesn't need to be an admin,
just the right to operate as as service.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 

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

Top