【发布时间】:2011-10-27 18:07:40
【问题描述】:
我有一个在 Windows 7 x64 上运行的 Visual Studio 2008 C# .NET 3.5 服务安装程序项目 (MSI)。
我订阅了ServiceInstaller.OnAfterInstall 通知以在安装完成后启动我的服务。
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
private System.ServiceProcess.ServiceInstaller my_installer_;
private void InitializeComponent()
{
// ...
this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
// ...
}
private void OnAfterInstall(object sender, InstallEventArgs e)
{
using (System.ServiceProcess.ServiceController svc =
new System.ServiceProcess.ServiceController("MyService"))
{
svc.Start(); // completes successfully
}
}
}
虽然该功能无一例外地成功,但安装程序完成后我的服务永远不会运行。
事件日志显示没有与服务启动相关的失败,如果我转到服务管理器,我可以手动启动服务(或重新启动 PC,它会自动启动)。
安装程序完成后我需要做什么才能自动启动我的服务?
【问题讨论】:
-
看起来你所做的应该有效。调试此问题的可能技巧:尝试插入值为 30 秒左右的
Thread.Sleep。它可能允许您将调试器附加到服务进程,并且您可以在Thread.Sleep之后设置一个断点。一旦您在调试器中暂停,如果您仍然在恢复时遇到错误,那么这不是时间问题。此时,您可以在闲暇时查询您的服务状态,并确定该步骤导致问题的原因。如果Thread.Sleep导致它开始工作,那当然是时间问题... -
也可以添加“System.Diagnostics.Debugger.Launch();”在您的 start 方法的开头强制附加调试器,以便您可以检查出了什么问题。有可能某处出现异常,您的服务只是自动停止。
标签: c# service installation