System.Security.SecurityException:未找到源,但无法搜索部分或全部事件日志。无法访问的日志:安全

发布于 2024-11-03 21:58:19 字数 1561 浏览 5 评论 0原文

我正在尝试创建一个 Windows 服务,但是当我尝试安装它时,它会回滚并给出以下错误:

System.Security.SecurityException: 未找到来源,但部分或全部 无法搜索事件日志。 无法访问的日志:安全性。

我不知道这意味着什么 - 我的应用程序只有最低限度,因为我只是先进行测试。

我的安装人员代码:

namespace WindowsService1
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;
            processInstaller.Username = null;
            processInstaller.Password = null;

            serviceInstaller.DisplayName = "My Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "My Service";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }
    }
}

我的服务代码:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        this.ServiceName = "My Service";
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        base.OnStop();
    }
}

I am trying to create a Windows Service, but when I try and install it, it rolls back giving me this error:

System.Security.SecurityException: The
source was not found, but some or all
event logs could not be searched.
Inaccessible logs: Security.

I don't know what this means - my application has the bare minimum since I am just testing things out first.

My Installer Code:

namespace WindowsService1
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;
            processInstaller.Username = null;
            processInstaller.Password = null;

            serviceInstaller.DisplayName = "My Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "My Service";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
        }
    }
}

My Service Code:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        this.ServiceName = "My Service";
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        base.OnStop();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

月亮是我掰弯的 2024-11-10 21:58:20

以管理员身份运行

这是程序员容易忽视的一个非常常见的问题。

Run as Administrator

This is a very common issue that programmers are missing out on.

黑色毁心梦 2024-11-10 21:58:20

安装服务

  1. 您可能正在尝试使用没有足够权限的用户帐户
    或者
  2. 具有管理员权限但未在“管理员模式”下运行命令提示符的用户。

具体来说,本例中的问题是在服务安装期间创建了一些 EventLog 注册表项。

解决此问题的一种方法是确保您在管理员模式下运行命令提示符。 (右键>以管理员身份运行)

我也遇到过一些情况,由于某些注册表项没有“完全控制”权限,此方法仍然无法解决SecurityException问题管理员帐户。

应为管理员设置以下键的“完全控制”,以便服务能够写入事件日志:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application

这可以通过以下方式完成:

  1. 调用 Windows 注册表编辑器
    • 运行 [Win + R]
    • 输入“regedit”
    • 好的
  2. 导航到上面列出的路径
  3. 右键单击​​所需路径
  4. 确保具有读取完全控制权限管理员复选框已勾选
  5. 单击应用确定
  6. 对其他路径重复相同的过程

You are probably trying to install a service using

  1. A user account which does not have sufficient rights
    OR
  2. A user with Administrator privileges but did not run the Command Prompt in 'Administrator Mode'.

Specifically, the issue in this case is the creation of some EventLog registry keys during service install.

One way to fix this is to make sure that you are running the Command Prompt in Administrator mode. (Right-click > Run as Administrator)

I have also encountered some cases where this method still fails to solve the SecurityException problem due to some registry keys not having 'Full Control' permissions for Administrator accounts.

The following keys should have 'Full Control' set for Administrators in order for the service to be able to write to the EventLog:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application

This may be done by:

  1. Invoking the Windows Registry Editor
    • Run [Win + R]
    • Type 'regedit'
    • OK
  2. Navigate to a path listed above
  3. Right click on the desired path
  4. Make sure that both Read and Full Control permission checkboxes are ticked for Administrators
  5. Click Apply and OK
  6. Repeat the same process for the other path
淤浪 2024-11-10 21:58:20

我通过使用管理权限打开 VS2013 开发人员控制台解决了同样的问题。

I solve this same issue by opening the VS2013 Developer Console with administrative permissions.

尴尬癌患者 2024-11-10 21:58:20

如果系统提示您输入用户名和密码,则说明某处的某些内容被设置为 Account = ServiceAccount.User - 这是唯一可能(应该)发生的情况。也许您上面注释中的代码没有被执行,或者它被稍后执行的代码更改回来。

至于你的第二段,一般来说,我认为如果你不希望它在控制台上看到或作为任务运行,那么服务就可以很好地实现这一点。我不确定我是否理解有关将其作为 ASP.NET 运行并不允许您查看数据库的部分...

最后,在您的最后一段中,我无法在不了解更多关于 NullException 的情况下谈论 NullException在您的安装程序代码中进行。

If you are being prompted for a user name and password, then something, somewhere is set to Account = ServiceAccount.User - that's the only way that could (should) happen. Perhaps your code in the comment above is not being executed or it is being changed back by later executing code.

As far as your second paragraph, in general, I would think a service would be fine for this if you don't want it to be see on the console or run as a task. I am not sure if I understand the part about running it as ASP.NET and having it not allow you to see the database...

Finally, in your last paragraph, I can't speak to the NullExeception without knowing more about what is going on in your installer's code.

时光与爱终年不遇 2024-11-10 21:58:20

我在尝试测试以管理员身份运行 VisualStudio 的事件日志是否存在时遇到此错误(OP 中的上方)

   if (!EventLog.SourceExists("applicatioName"))
         EventLog.CreateEventSource("applicatioName", "Application");

解决了该问题。

I was getting this error(above in the OP) while trying to test for the existence of an EventLog

   if (!EventLog.SourceExists("applicatioName"))
         EventLog.CreateEventSource("applicatioName", "Application");

Running VisualStudio as Administrator resolved the issue.

著墨染雨君画夕 2024-11-10 21:58:20

就我而言,我在 ProjectInstaller 类中遇到了问题,我尝试更改服务的属性,一旦编码错误,此属性就会生成对象引用错误。一旦我获得了正确的代码,System.Security.SecurityException 错误就得到了解决。

In my case, I had a problem in the ProjectInstaller class, I tried to change a property of the service and this attribution generated an object reference error once the coding was wrong. Once I got the right code, the System.Security.SecurityException error was solved.

妄想挽回 2024-11-10 21:58:19

使用 installutil。解决方案是以管理员身份打开命令行,然后运行 ​​installutil。

此外,您可能会发现使用像 TopShelf 这样的框架来托管您的服务更容易,因为它管理所有 设置配置,从服务的名称和描述到恢复过程的工作方式。当您调试服务时,它还允许从 IDE 内部轻松启动服务。

I got the same exception when trying to install a service from the command line when using installutil in Windows 7. The solution was to open the command line as Administrator and then run installutil.

Also you may find it easier to use a framework like TopShelf to host your services, because it manages all the setup configuration from the name and description of the service to how your recovery process will work. It also allows to easily start your service from inside the IDE when you are debugging it.

有木有妳兜一样 2024-11-10 21:58:19

以管理员身份运行命令提示符。
它会解决你的问题

Run your command prompt as administrator.
It will solve your problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文