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

Sha*_*ica 3 c# windows-services

@Marc Gravell给出了如何在这里安装Windows服务的一个很好的例子.我去实施它,一切都很好.

然后我重新启动了我的电脑......突然间,当我尝试安装时,我开始获得安全异常!我得到一个SecurityException:"不允许请求注册表访问".我想也许麻烦从重新启动开始,所以就像在头部的第二次打击治疗健忘症的漫画中,我尝试重新启动...但事实证明生活不像卡通...... :(

好的,所以我用Google搜索了问题,并找到了建议,将注册表项HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog的权限授予我的网络服务.这也行不通.我赋予每个人完全的权利 - 而且是HURRAY!我现在得到一个不同的例外! InvalidOperationException:"无法在计算机上打开服务控制管理器.".此操作可能需要其他权限.(内部异常是Win32Exception:"访问被拒绝".)嗯,对不起?我正在尝试在本地计算机上安装!什么是"电脑".""那边做什么?

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

这是我安装的代码(复制并改编自Marc Gravell的样本):

using (var inst = new AssemblyInstaller(typeof(MyNamespace.Program).Assembly, new string[] { })) {
  IDictionary state = new Hashtable();
  inst.UseNewContext = true;
  try {
    if (uninstall) {
      inst.Uninstall(state);
    } else {
      inst.Install(state);
      inst.Commit(state);
    }
  } catch {
    try {
      inst.Rollback(state);
    } catch { }
    throw;
  }
}
Run Code Online (Sandbox Code Playgroud)

安装程序代码是:

[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;
  }
}
Run Code Online (Sandbox Code Playgroud)

这可能有什么问题?为什么事情在他们事先正常工作之后突然开始变得混乱?

fej*_*oco 8

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