473,566 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Service Terminating Itself

I have a C# Windows Service running as the NetworkService account because it
needs to access a network share.

As part of the service's initialization, I want the service to terminate, if
an unrecoverable error occurs. When that case occurs, I create a
ServiceControll er object and call the Stop() method.

However - I get an exception thrown saying access denied. If I switch to
using the LocalService account it works fine, but I lose access to my
network resources.

What is the proper way for a service to stop itself? I would really rather
not add a separate user account to accomplish this. It seems kind of odd
that a service cannot stop itself.
Any ideas?

--
Adam Clauss

Nov 17 '05 #1
23 7560
> However - I get an exception thrown saying access denied. If I switch to
using the LocalService account it works fine, but I lose access to my
network resources.
Looks like LocalService has rights to stop services, but NetworkService
does not. Sounds logical enough: the method you use can be used to stop
any service, not just itself.
What is the proper way for a service to stop itself? I would really
rather
not add a separate user account to accomplish this. It seems kind of odd
that a service cannot stop itself.
Any ideas?

Did you try Application.Exi t() ?

Greetings,
Wessel
Nov 17 '05 #2
"Wessel Troost" <no*****@like.t he.sun> wrote in message
news:op.st9qu6x af3yrl7@asbel.. .
However - I get an exception thrown saying access denied. If I switch to
using the LocalService account it works fine, but I lose access to my
network resources.
Looks like LocalService has rights to stop services, but NetworkService
does not. Sounds logical enough: the method you use can be used to stop
any service, not just itself.
What is the proper way for a service to stop itself? I would really
rather
not add a separate user account to accomplish this. It seems kind of odd
that a service cannot stop itself.
Any ideas?

Did you try Application.Exi t() ?


Would be great, but that only exists in a Windows.Forms context.

--
Adam Clauss

Greetings,
Wessel

Nov 17 '05 #3
Initialize your service in OnStart and throw an exception if it fails.
Throwing an exception will terminate the service, report an error to the SCM
and automatically log the exception message in the Application log.

Willy.

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.com...
"Wessel Troost" <no*****@like.t he.sun> wrote in message
news:op.st9qu6x af3yrl7@asbel.. .
However - I get an exception thrown saying access denied. If I switch
to
using the LocalService account it works fine, but I lose access to my
network resources.

Looks like LocalService has rights to stop services, but NetworkService
does not. Sounds logical enough: the method you use can be used to stop
any service, not just itself.
What is the proper way for a service to stop itself? I would really
rather
not add a separate user account to accomplish this. It seems kind of
odd
that a service cannot stop itself.
Any ideas?

Did you try Application.Exi t() ?


Would be great, but that only exists in a Windows.Forms context.

--
Adam Clauss

Greetings,
Wessel


Nov 17 '05 #4
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uT******** ******@TK2MSFTN GP09.phx.gbl...
Initialize your service in OnStart and throw an exception if it fails.
Throwing an exception will terminate the service, report an error to the
SCM and automatically log the exception message in the Application log.
Is that considered a "good" way to terminate a service? It's also possible
that sometime down the road (post-initialization) that we may want to
terminate. Unfortunately, "down the road' for our application means from
one of several threads. Throwing an exception here will not actually
terminate the service. Is there a way to end it from there?

It's amazing... I never though I would have trouble trying to END an
application :)

--
Adam Clauss


Willy.

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.com...
"Wessel Troost" <no*****@like.t he.sun> wrote in message
news:op.st9qu6x af3yrl7@asbel.. .
However - I get an exception thrown saying access denied. If I switch
to
using the LocalService account it works fine, but I lose access to my
network resources.

Looks like LocalService has rights to stop services, but NetworkService
does not. Sounds logical enough: the method you use can be used to stop
any service, not just itself.

What is the proper way for a service to stop itself? I would really
rather
not add a separate user account to accomplish this. It seems kind of
odd
that a service cannot stop itself.
Any ideas?

Did you try Application.Exi t() ?


Would be great, but that only exists in a Windows.Forms context.

--
Adam Clauss

Greetings,
Wessel



Nov 17 '05 #5
> Is that considered a "good" way to terminate a service? It's also

What Willy says sounds very good, definitely the way to go.
possible
that sometime down the road (post-initialization) that we may want to
terminate. Unfortunately, "down the road' for our application means from
one of several threads. Throwing an exception here will not actually
terminate the service. Is there a way to end it from there?

Eeh.... maybe this works:

Environment.Exi t()

or if you're in a bad mood:

Process.GetCurr entProcess().Ki ll();

Greetings,
Wessel
Nov 17 '05 #6

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.com...
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uT******** ******@TK2MSFTN GP09.phx.gbl...
Initialize your service in OnStart and throw an exception if it fails.
Throwing an exception will terminate the service, report an error to the
SCM and automatically log the exception message in the Application log.


Is that considered a "good" way to terminate a service? It's also
possible that sometime down the road (post-initialization) that we may
want to terminate. Unfortunately, "down the road' for our application
means from one of several threads. Throwing an exception here will not
actually terminate the service. Is there a way to end it from there?


No it isn't, you aren't terminating a "running" service, for the SCM, the
service is considered to be "started" once the OnStart() has returned
successfully, you throw an exception which indicates to the SCM that the
service failed to start.
A clean way to stop a "running" Service, is by asking the SCM to issue a
Stop request (using the ServiceControle r object for instance), but you need
to do this from an external program that has sufficient privileges to do so.
The external program (lets name it the controller) should set-up an IPC
channel (using remoting or whatever) over which the Service can communicate
it's internal statuses and request actions from the controller.

Willy.

Nov 17 '05 #7

"Wessel Troost" <no*****@like.t he.sun> wrote in message
news:op.st9urwk 4f3yrl7@asbel.. .
Is that considered a "good" way to terminate a service? It's also


What Willy says sounds very good, definitely the way to go.
possible
that sometime down the road (post-initialization) that we may want to
terminate. Unfortunately, "down the road' for our application means from
one of several threads. Throwing an exception here will not actually
terminate the service. Is there a way to end it from there?

Eeh.... maybe this works:

Environment.Exi t()

or if you're in a bad mood:

Process.GetCurr entProcess().Ki ll();

Greetings,
Wessel


Wessel,

This is something you should avoid, the reason is simple, the Service
Control Manager is the master of the game and he doesn't like someone else
is playing with it's toys, so when you kill a process that happens to be a
service you are effectively disturbing the SCM and possibly corrupting it's
DB state.
How many times did you see messages like "Service cannot be started because
it's already running" while in reality it's not?

Willy.

Nov 17 '05 #8
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:u6******** *****@TK2MSFTNG P15.phx.gbl...
Environment.Exi t()

or if you're in a bad mood:

Process.GetCurr entProcess().Ki ll();

Greetings,
Wessel


Wessel,

This is something you should avoid, the reason is simple, the Service
Control Manager is the master of the game and he doesn't like someone else
is playing with it's toys, so when you kill a process that happens to be a
service you are effectively disturbing the SCM and possibly corrupting
it's DB state.
How many times did you see messages like "Service cannot be started
because it's already running" while in reality it's not?


Thats why I was afraid of using the Exception initially. Thought it might
cause a similar situation.

--
Adam Clauss
Nov 17 '05 #9
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:OJ******** ******@tk2msftn gp13.phx.gbl...

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.com...
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uT******** ******@TK2MSFTN GP09.phx.gbl...
Initialize your service in OnStart and throw an exception if it fails.
Throwing an exception will terminate the service, report an error to the
SCM and automatically log the exception message in the Application log.


Is that considered a "good" way to terminate a service? It's also
possible that sometime down the road (post-initialization) that we may
want to terminate. Unfortunately, "down the road' for our application
means from one of several threads. Throwing an exception here will not
actually terminate the service. Is there a way to end it from there?


No it isn't, you aren't terminating a "running" service, for the SCM, the
service is considered to be "started" once the OnStart() has returned
successfully, you throw an exception which indicates to the SCM that the
service failed to start.
A clean way to stop a "running" Service, is by asking the SCM to issue a
Stop request (using the ServiceControle r object for instance), but you
need to do this from an external program that has sufficient privileges to
do so. The external program (lets name it the controller) should set-up an
IPC channel (using remoting or whatever) over which the Service can
communicate it's internal statuses and request actions from the
controller.


Hehe, what's interesting is that we may already have such a thing, but I've
been told I cannot "expect" it to necessarily be present (clients may elect
whether or not to use this other service).

It looks like given the constraints of the project, I may have to actually
set it up to run as a user account (rather than Network Service). It's the
only way to satisfy all my needs.

Thanks for the help, you've given me some things to think about.

--
Adam Clauss
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7243
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service stopping. Please advise how to stop the service. Thanks, SP
8
1419
by: Jerry Camel | last post by:
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...
20
1402
by: Lee Schipper | last post by:
If a service needs to shut itself down, say due to a fatal error detected in the program, what is the cleanest way to do that? Can I simply use the Visual Basic End statement? I only have a single service running in my process. Thanks!
0
2041
by: Kevin | last post by:
I'm writing a service in VB.NET. Reference the following code: Dim oStreamWriter As StreamWriter .... .... .... oStreamWriter = File.CreateText(TempLogFile) If Err.Number <> 0 Then EventLog.WriteEntry("LogService", "Error Creating Log File: " & _ TempLogFile & vbCrLf & _
0
7686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7650
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.