C# 使用AssemblyInstaller安装Windows服务时出现问题

C# 使用AssemblyInstaller安装Windows服务时出现问题,c#,windows-services,C#,Windows Services,@Marc Gravell给出了一个安装Windows服务的好例子。我去实施了,一切都很好 然后我重新启动了我的电脑。。。当我尝试安装时,突然发现安全异常!我得到一个安全性异常:“不允许请求的注册表访问”。我想问题可能是从重新启动开始的,所以就像在卡通片中第二次击中头部可以治愈健忘症一样,我尝试重新启动。。。但事实证明生活不像卡通片( 好的,我用谷歌搜索了这个问题,找到了一些建议,将注册表项HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\servi

@Marc Gravell给出了一个安装Windows服务的好例子。我去实施了,一切都很好

然后我重新启动了我的电脑。。。当我尝试安装时,突然发现安全异常!我得到一个
安全性异常
:“不允许请求的注册表访问”。我想问题可能是从重新启动开始的,所以就像在卡通片中第二次击中头部可以治愈健忘症一样,我尝试重新启动。。。但事实证明生活不像卡通片(

好的,我用谷歌搜索了这个问题,找到了一些建议,将注册表项HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\services\eventlog的权限授予我的网络服务。这也不起作用。我把所有人的权限都授予了——好哇!我现在得到了一个不同的异常!
InvalidoOperationException
:“无法在计算机“”上打开服务控制管理器。此操作可能需要其他权限。”(内部异常为
Win32Exception
:“访问被拒绝”。)嗯,对不起?我正在尝试在本地计算机上安装!什么是“计算机”。“在那里做什么?”

这是非常令人沮丧的,因为正如我所说的,昨天它工作得很好,而今天一切都崩溃了,代码库没有任何明显的变化

以下是我的安装代码(从Marc Gravell的示例中复制和改编):

安装程序代码为:

[RunInstaller(true)]
public sealed class MyServiceInstallerProcess : ServiceProcessInstaller {
  public MyServiceInstallerProcess() {
    this.Account = ServiceAccount.NetworkService;
  }
}

[RunInstaller(true)]
public sealed class MyServiceInstaller : ServiceInstaller {
  public MyServiceInstaller() {
    this.Description = "My service desc";
    this.DisplayName = "My service name";
    this.ServiceName = "My service name";
    this.StartType = ServiceStartMode.Automatic;
  }
}

这里可能有什么问题?为什么事情突然开始失控,在他们之前工作正常后?

以管理员身份运行安装程序/具有提升的权限。让每个人都可以访问系统注册表项和其他内容是非常错误的。

啊,就是这样。在我离开之前,出于某种或其他原因,我在管理模式下打开了VS开始在安装程序上工作,这就是为什么当时一切正常。当我重新启动时,我以常规模式打开了VS,并失去了管理员权限。谢谢!
[RunInstaller(true)]
public sealed class MyServiceInstallerProcess : ServiceProcessInstaller {
  public MyServiceInstallerProcess() {
    this.Account = ServiceAccount.NetworkService;
  }
}

[RunInstaller(true)]
public sealed class MyServiceInstaller : ServiceInstaller {
  public MyServiceInstaller() {
    this.Description = "My service desc";
    this.DisplayName = "My service name";
    this.ServiceName = "My service name";
    this.StartType = ServiceStartMode.Automatic;
  }
}