如何让正确安装后一个.NET Windows服务启动?正确、NET、Windows

2023-09-02 10:14:55 作者:今朝有酒今朝醉

除了service.StartType = ServiceStartMode.Automatic我的服务安装后不会启动

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

解决方案

插入此code对我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和弗朗西斯湾

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