为什么我的服务在安装后没有启动
本文关键字:启动 安装 我的 服务 为什么 | 更新日期: 2023-09-27 18:36:11
>我创建了一个Windows服务,它设置为自动启动。我还在安装程序中添加了以下代码:
public ProjectInstaller()
{
InitializeComponent();
serviceProcessInstaller1.AfterInstall += new InstallEventHandler(serviceProcessInstaller1_AfterInstall);
}
void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
try
{
var sc = new ServiceController(serviceInstaller1.ServiceName);
sc.Start();
}
catch
{
}
}
protected override void OnCommitted(IDictionary savedState)
{
try
{
var sc = new ServiceController(serviceInstaller1.ServiceName);
sc.Start();
}
catch
{
}
}
服务已正确安装,但永远不会启动。
这可能是什么原因?
也许您需要输入一些临时诊断日志记录,也许使用 System.IO.File.WriteAllText();
.我知道这不是你要找的答案,但它可能会给你最快的解决方案!
try
{
var sc = new ServiceController(serviceInstaller1.ServiceName);
sc.Start();
System.IO.File.WriteAllText(@"c:'temp'servicestart.txt", "Service started");
}
catch (Exception ex)
{
System.IO.File.WriteAllText(@"c:'temp'servicestart.txt", ex.Message);
}
确保在Main()
函数中有以下行:
ServiceBase.Run(new ServiceClass());
我有几次离开感到内疚 Application.Run(new Class());
(如果你是从 Windows 窗体应用开始的)
我不久前创建了一个服务,我与您的服务的不同之处在于您以这种方式声明
var sc = new ServiceController(serviceInstaller1.ServiceName);
我的而不是采用服务安装程序1.服务名称我通过这样的简单字符串给出名称
var sc = new ServiceController("MyService");
我认为这根本不是问题,但是在谈论服务时,一切都值得一试
编辑:现在看看它,我已经看到我使用的名称实际上是 DisplayName 而不是服务名称,请尝试手动传递它或使用 serviceInstaller1.DisplayName
它是否可能依赖于另一个服务。 或者您是否尝试过延迟启动?
这对
我有用
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName);
sc.Start();
}