【发布时间】: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() 在...(我的代码详细信息)我不明白为什么,因为:
- 服务名称与
ServiceInstaller中的名称完全相同(在ServiceName属性) - 代码在不同的服务中执行,该服务在 本地系统帐户。
【问题讨论】:
-
你有没有得到这个答案?
-
@TCopple,在 stackoverflow 里面——显然不是。外面——也许,我不记得了。但我不这么认为,否则我会在这里回答自己。
标签: .net service installation servicecontroller