【问题标题】:Automatically starting a service when the installer finishes [duplicate]安装程序完成时自动启动服务[重复]
【发布时间】:2011-10-27 18:07:40
【问题描述】:

可能重复:
How to automatically start your service after install?

我有一个在 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


【解决方案1】:

使用 AfterInstall 事件

在您的服务安装程序类中创建AfterInstall 事件并使用ServiceController 启动服务。

public ServiceInstaller()
{
    InitializeComponent();
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
    sc.Start();
}

使用提交事件

public ServiceInstaller()
{
    InitializeComponent();
    this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}

void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
    sc.Start();
}

或者您可以覆盖 OnCommitted 事件

    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        base.OnCommitted(savedState);
        new ServiceController(serviceInstaller1.ServiceName).Start();
    }

上述以外请查看以下内容

  • 安装程序启动类型:自动
  • 帐户:本地系统

除了服务安装程序,您还需要通过提供上述服务安装程序的主要输出创建的安装项目。

在设置中,通过提供服务安装程序项目输出,至少在安装时创建自定义操作。

更多信息来自here

【讨论】:

    【解决方案2】:

    我假设 Start 立即返回,并在后台启动服务。查看文档:http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx

    【讨论】:

    • 是的,我敢肯定。这并不能解释为什么服务没有启动或如何正确启动它。
    • 您是否尝试过等到它启动?我发布的链接中有一个示例
    • 等待并没有解决问题 - 安装程序挂起等待。该服务将尝试启动并失败。我在Installer.Install 覆盖的顶部有base.Install(savedState);。因此,安装程序在完成安装所有依赖项之前尝试启动服务。通过将基本调用移动到我的覆盖的底部,我解决了这个问题。任何人都无法从我发布的内容中看到任何内容,因此您获得积分是因为您引导我找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多