C#中:简单的Windows服务提供了安全例外 [英] C#: Simple Windows Service gives Security Exception

查看:145
本文介绍了C#中:简单的Windows服务提供了安全例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了演练在下面的链接:
HTTP ://msdn.microsoft.com/en-us/library/zt39148a%28VS.80%29.aspx

I am doing the walkthrough in the following link: http://msdn.microsoft.com/en-us/library/zt39148a%28VS.80%29.aspx

我已经完全遵循它,行按行。我成功地安装该服务,但是,当我尝试运行它,将出现以下错误消息:

I have followed it exactly, line by line. I installed the service successfully, however, when I try to run it, the following error message appears:

未处理的异常(System.Security.SecurityException')发生在MyNewService.Exe [5292]

"An unhandled exception ("System.Security.SecurityException') occurred in MyNewService.Exe [5292].

我已经看到,对于很多人来说它的工作原理,但有些人得到这个例外,虽然我无法找到答案。没有人有一个想法?谢谢你。

I have seen that for many people it works, but some people get this exception, though I could not find an answer. Does anyone have an idea? Thanks.

推荐答案

EventLog.SourceExists 方法是将导致此异常。最常见的原因是它尝试访问所有默认在Vista中,你将不会有权限事件日志(包括安全日志)。 (!我觉得这相当奇怪)。另一个原因可能是,如果你正在寻找的源在事件日志中没有找到

The EventLog.SourceExists method is what will be causing this exception. The most common reason being it tries to access ALL the event logs (including the Security log) which by default in Vista you will not have permissions for. Another reason can be if the source you are looking for is not found in the event log (which I find rather odd!).

一个解决方法:

bool sourceFound = false;
try
{
    sourceFound = EventLog.SourceExists("MySource");
}
catch (SecurityException)
{
    sourceFound = false;
}



另一种选择是简单地提升您的权限,但是,由于你在哪里继教程一步一步的服务将LocalService帐户下运行(这将再次没有权限对这个特殊的方法)。因此,你会发现MSDN文档解决方案是检查在的ServiceInstaller是否存在事件源,如果没有,请在安装程序的来源。

Another option is to simply elevate your permissions, however, as you where following the tutorial step by step your service would be running under the LocalService account (which again won't have permissions for this particular method). Hence, you will find on the MSDN documentation the solution is to check whether the event source exists in the ServiceInstaller and if it doesn't, create the source in the installer.

这篇关于C#中:简单的Windows服务提供了安全例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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