2008-10-17 124 views
50

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

我只是想出了这一个,认为我会分享一般的好的答案。回答按照。我愿意接受其他更好的方式来做到这一点。

+2

我很高兴看到有人发布有用的问题,他们知道答案。有时你只知道你的建议将受到欢迎。也有可能有人会用你的解决方案的有吸引力的替代方案回复。 – DOK 2008-10-17 16:25:38

+0

这正是我所希望的。 – 2008-10-17 16:28:00

+0

这是一件很明显的事情要做。它永远不会让我惊讶于微软在代码中遗漏的东西。 – 2009-06-16 13:38:37

回答

54

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

using System.ServiceProcess; 

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

设置项目将选取类并在安装程序完成后运行您的服务。

+3

ServiceController实现IDisposable。没有使用'使用'关键字或调用Dispose方法故意? – 2009-06-03 23:53:16

+0

我同意妥善处置总是一个好主意。在这种情况下,它只运行一次。 OnCommitted在安装程序运行后触发,然后服务像其他服务一样进行管理,并在下次重新启动时自动启动。 – 2009-06-04 12:48:52

9

由于它运行OK ...

private System.ServiceProcess.ServiceInstaller serviceInstaller1; 

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

小除了接受的答案:

你也可以下载这样的服务名称 - 避免任何问题,如果服务的名称在将来改变:

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

(每个安装有一个ServiceProcessInstaller和的ServiceInstaller。这里的ServiceInstaller称为serviceInstaller1。)

7

而不是创建自己的类中,选择项目安装服务安装程序并添加事件处理程序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(); 
    } 
} 

,它会开始只有启动类型设置为自动为您服务。

3

根据上面的代码片段,我的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(); 
     } 
    } 
} 
19

这种方法使用的安装程序和类代码量最少。

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(); 
     } 
    } 
} 

定义的安装程序类设计serviceInstaller1(类型的ServiceInstaller),并在设计器中设置其属性ServiceName

相关问题