473,704 Members | 2,812 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 7581
> 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
7268
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
1427
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 within the service itself? Thanks. Jerry
20
1418
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
2051
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
8767
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8687
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8977
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6605
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4439
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4701
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2483
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.