如何在安装后立即启动 .NET Windows 服务? [英] How to make a .NET Windows Service start right after the installation?

查看:29
本文介绍了如何在安装后立即启动 .NET Windows 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 service.StartType = ServiceStartMode.Automatic 我的服务在安装后没有启动

Besides the service.StartType = ServiceStartMode.Automatic my service does not start after installation

解决方案

在我的 ProjectInstaller 上插入此代码

Inserted this code on my ProjectInstaller

protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
    base.OnAfterInstall(savedState);
    using (var serviceController = new ServiceController(this.serviceInstaller1.ServiceName, Environment.MachineName))
        serviceController.Start();
}

感谢 ScottTx 和 Francis B.

Thanks to ScottTx and Francis B.

推荐答案

您可以在服务可执行文件中执行所有这些操作,以响应从 InstallUtil 进程触发的事件.覆盖 OnAfterInstall 事件以使用 ServiceController 类来启动服务.

You can do this all from within your service executable in response to events fired from the InstallUtil process. Override the OnAfterInstall event to use a ServiceController class to start the service.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx

这篇关于如何在安装后立即启动 .NET Windows 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆