【问题标题】:How to automatically start your service after install?安装后如何自动启动服务?
【发布时间】:2008-10-17 15:51:44
【问题描述】:

从 Visual Studio 安装项目运行安装后如何自动启动服务?

我只是想出了这个问题,并认为我会为了大众的利益分享答案。回答跟随。我愿意接受其他更好的方法。

【问题讨论】:

  • 我很高兴看到有人发布一个他们知道答案的有用问题。有时你只知道你的小费会受到欢迎。也有可能有人会回复您的解决方案的有吸引力的替代方案。
  • 这正是我所希望的。
  • 这是一件显而易见的事情。微软从他们的代码中遗漏的东西总是让我感到惊讶。

标签: .net installation service


【解决方案1】:

将以下类添加到您的项目中。

using System.ServiceProcess;  

class ServInstaller : ServiceInstaller
{
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        ServiceController sc = new ServiceController("YourServiceNameGoesHere");
        sc.Start();
    }
}

安装程序完成后,安装项目将选择课程并运行您的服务。

【讨论】:

  • ServiceController 实现 IDisposable。没有使用“using”关键字或有意调用 Dispose 方法?
  • 我同意妥善处理总是一个好主意。在这种情况下,它只运行一次。 OnCommitted 在安装程序运行后触发,然后像其他任何服务一样管理服务,并在下次重新启动时自动启动。
  • base.OnCommitted(...) 怎么样。需要调用吗?
  • 您可以使用 Committed 或 AfterInstall 事件,而不是创建新类。请参阅下面的答案。
【解决方案2】:

对已接受答案的小补充:

您也可以像这样获取服务名称 - 避免将来更改服务名称时出现任何问题:

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

(每个Installer都有一个ServiceProcessInstaller和一个ServiceInstaller。这里ServiceInstaller叫做serviceInstaller1。)

【讨论】:

  • serviceInstaller1 是 ProjectInstaller 的私有成员变量,那么我该如何从 ServInstaller 访问它?
【解决方案3】:

这种方法使用 Installer 类和最少的代码。

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace MyProject
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
        }
    }
}

在 Installer 类设计器中定义 serviceInstaller1(类型 ServiceInstaller),并在设计器中设置其 ServiceName 属性。

【讨论】:

  • 我必须将此代码放在 Committed 事件中。似乎我的服务在 OnAfterInstall 尚不可用。
【解决方案4】:

感谢它运行正常...

private System.ServiceProcess.ServiceInstaller serviceInstaller1;

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController("YourServiceName");
    sc.Start();
}

【讨论】:

  • 这实际上是最简单的方法,因为服务安装程序和 AfterInstall 事件已经存在 - 无需添加新类。
【解决方案5】:

不要创建自己的类,而是在项目安装程序中选择服务安装程序并将事件处理程序添加到 Comitted 事件:

private void serviceInstallerService1_Committed(object sender, InstallEventArgs e)
{
    var serviceInstaller = sender as ServiceInstaller;
    // Start the service after it is installed.
    if (serviceInstaller != null && serviceInstaller.StartType == ServiceStartMode.Automatic)
    {
        var serviceController = new ServiceController(serviceInstaller.ServiceName);
        serviceController.Start();
    }
}

只有在启动类型设置为自动时才会启动你的服务。

【讨论】:

  • 别忘了加:using System.ServiceProcess;
  • 这是最适合我的选项,似乎是最简单和最明显的选择
【解决方案6】:

根据上面的 sn-ps,我的 ProjectInstaller.cs 文件对于名为 FSWServiceMgr.exe 的服务看起来像这样。该服务确实在安装后启动。附带说明一下,当在解决方案资源管理器中选择设置项目以设置公司等时,请记住单击属性选项卡(不是右键单击)。


using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace FSWManager {
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer {
        public ProjectInstaller() {
            InitializeComponent();
            this.FSWServiceMgr.AfterInstall += FSWServiceMgr_AfterInstall;
        }

        static void FSWServiceMgr_AfterInstall(object sender, InstallEventArgs e) {
            new ServiceController("FSWServiceMgr").Start();
        }
    }
}

【讨论】:

    【解决方案7】:

    还有另一种不涉及代码的方式。您可以使用服务控制表。使用 orca.exe 编辑生成的 msi 文件,并将条目添加到 ServiceControl Table

    只有 ServiceControl、Name、Event 和 Component_ 列是必需的。 Component_ 列包含文件表中的 ComponentId。 (选择文件表中的File,将Component_value复制到ServiceControl表中。)

    最后一步是将InstallExecutesequence表中StartServices的值更新为6575。这足以启动服务。

    顺便说一下,服务安装表允许你配置安装程序来安装windows服务。

    【讨论】:

    • 红旗!一切都很好,花花公子,可能很高兴潜入 msi 包以进行学习和其他东西,但这将成为发布更新时的负担,因为每次生成 msi 包时都必须手动完成所有这些工作。有人会在某个时候忘记这一点,如果你一段时间不推送更新,它就会被遗忘。
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多