C#服务终止自身 [英] C# Service Terminating Itself

查看:60
本文介绍了C#服务终止自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows服务作为NetworkService帐户运行,因为它需要访问网络共享。


作为服务的一部分''初始化,我希望服务终止,如果

发生不可恢复的错误。当这种情况发生时,我创建一个

ServiceController对象并调用Stop()方法。


但是 - 我得到一个异常抛出,说访问被拒绝。如果我使用LocalService帐户切换到

它工作正常,但我无法访问我的

网络资源。


什么服务停止的正确方法是什么?我宁愿

也不要添加单独的用户帐户来完成此任务。服务无法阻止,这似乎有点奇怪



有什么想法吗?


-

Adam Clauss

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
ServiceController 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

推荐答案

>但是 - 我得到一个异常抛出说访问被拒绝。如果我使用LocalService帐户切换到
> However - I get an exception thrown saying access denied. If I switch to
,它可以正常工作,但我无法访问我的网络资源。

看起来LocalService有权停止服务,但NetworkService

没有。听起来合乎逻辑:你使用的方法可以用来停止任何服务,而不仅仅是自己。

服务停止的正确方法是什么?我真的不想添加单独的用户帐户来完成此任务。一个服务无法阻止它似乎有点奇怪。
任何想法?
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?



您是否尝试过Application.Exit()?


问候,

Wessel


Did you try Application.Exit() ?

Greetings,
Wessel


" Wessel Troost" <无***** @ like.the.sun>在消息中写道

news:op.st9qu6xaf3yrl7@asbel ...
"Wessel Troost" <no*****@like.the.sun> wrote in message
news:op.st9qu6xaf3yrl7@asbel...
但是 - 我得到一个异常抛出说拒绝访问。如果我使用LocalService帐户切换到
它工作正常,但我无法访问我的网络资源。
看起来LocalService有权停止服务,但NetworkService
不。听起来很合理:你使用的方法可以用来停止任何服务,而不仅仅是它自己。
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?


您是否尝试过Application.Exit()?


Did you try Application.Exit() ?




会很棒,但只存在于Windows.Forms上下文中。


-

Adam Clauss


问候,
Wessel



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

--
Adam Clauss

Greetings,
Wessel



在OnStart中初始化您的服务,如果失败则抛出异常。

抛出异常将终止服务,向SCM报告错误

并自动在应用程序日志中记录异常消息。


Willy 。


" Adam Clauss" < CA ***** @ tamu.edu>在消息中写道

news:11 ************* @ corp.supernews.com ...
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.edu> wrote in message
news:11*************@corp.supernews.com...
" Wessel Troost" <无***** @ like.the.sun>在消息中写道
新闻:op.st9qu6xaf3yrl7@asbel ...
"Wessel Troost" <no*****@like.the.sun> wrote in message
news:op.st9qu6xaf3yrl7@asbel...
但是 - 我得到一个异常抛出,说拒绝访问。如果我使用LocalService帐户切换
它可以正常工作,但我无法访问我的网络资源。
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.


看起来LocalService有权停止服务,但NetworkService没有。听起来很合理:你使用的方法可以用来停止任何服务,而不仅仅是它自己。


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?


您是否尝试过Application.Exit()?


Did you try Application.Exit() ?



会很棒,但只存在于Windows.Forms上下文中。

-




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

--
Adam Clauss

问候,
Wessel
Greetings,
Wessel




这篇关于C#服务终止自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆