【问题标题】:Problem with installing Windows service using AssemblyInstaller使用 AssemblyInstaller 安装 Windows 服务时出现问题
【发布时间】:2011-06-01 11:39:03
【问题描述】:

@Marc Gravell 给出了如何安装 Windows 服务 here 的一个很好的示例。我去实现了,一切都很好。

然后我重新启动了我的计算机......当我尝试安装时突然开始出现安全异常!我收到SecurityException:“不允许请求的注册表访问”。我以为问题可能是从重启开始的,所以就像漫画中第二次打击头部治愈失忆的漫画一样,我再次尝试重启......但事实证明生活不像漫画......:(

好的,所以我搜索了这个问题,并找到了将注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog 的权限授予我的网络服务的建议。那也没用。我给予每个人全部权利 - 和 HURRAH!我现在遇到了一个不同的例外! 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;
  }
}

安装程序代码为:

[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;
  }
}

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

【问题讨论】:

    标签: c# windows-services


    【解决方案1】:

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

    【讨论】:

    • 啊,就是这样。在我开始使用安装程序之前,我出于某种或其他原因以管理员模式打开了 VS,这就是为什么一切正常的原因。当我重新启动时,我以常规模式打开 VS,并失去了管理员权限。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多