【问题标题】:Service not found after installing using AssemblyInstaller使用 AssemblyInstaller 安装后找不到服务
【发布时间】:2012-09-06 06:49:50
【问题描述】:

上下文

Windows 2008 64 位。
我安装了一个充当安装程序的 .NET 服务。

背景

我正在使用此代码(来源: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;
    }
}

问题

一切正常,没有异常,但在那之后,我尝试运行以下代码来启动刚刚安装的服务:

using (var sc = new ServiceController("the service's name"))
{
    sc.Start();
    sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(20));
}

我得到一个例外:

System.InvalidOperationException:在计算机“。”上找不到服务 [服务名称在此处]。 ---> System.ComponentModel.Win32Exception: 指定的服务不作为已安装的服务存在 --- 内部异常堆栈跟踪结束 --- 在 System.ServiceProcess.ServiceController.GenerateNames() 在 System.ServiceProcess.ServiceController.get_ServiceName() 在 System.ServiceProcess.ServiceController.Start(字符串 [] 参数) 在 System.ServiceProcess.ServiceController.Start() 在...(我的代码详细信息)

我不明白为什么,因为:

  1. 服务名称与ServiceInstaller 中的名称完全相同(在 ServiceName 属性)
  2. 代码在不同的服务中执行,该服务在 本地系统帐户。

【问题讨论】:

  • 你有没有得到这个答案?
  • @TCopple,在 stackoverflow 里面——显然不是。外面——也许,我不记得了。但我不这么认为,否则我会在这里回答自己。

标签: .net service installation servicecontroller


【解决方案1】:

有一个安装日志可能会有所帮助。在服务文件夹中查找YouServiceName.InstallLog 文件。

为服务添加安装程序就足够了(对我来说)。你应该:

  • 在设计器中打开Service.cs文件,
  • 右键单击它,然后
  • 选择菜单选项“添加安装程序”。

它不会立即安装...您需要先创建安装程序类。

有关参考,另请参阅this question

【讨论】:

    【解决方案2】:

    可能是服务卡在等待状态,还没有完成注册,所以无法识别为已安装服务。
    当您设置 UseNewContext 属性时 - 安装日志文件(“{Assembly name}.InstallLog”)是否包含任何有意义的信息?
    另外,要检查这是否是权限问题,您可以尝试使用

    验证服务是否存在
    sc query <ServiceName>
    

    从管理命令窗口?

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多