VS Setup - 关于Windows Service

来源:互联网 发布:免费的收音机不用网络 编辑:程序博客网 时间:2024/06/05 03:35

前面写过一篇使用VS Setup项目作安装包的总结,今天补充两点关于打包Windows Service的tips:

1. 如何安装windows service? - 使用service installer

2. 如何在安装向导完成后,启动刚安装完成的service?

   2.1 使用Custom Action并在Installer的Committed事件中启动service

   2.2 将此Custom Action 添加到安装项目的Custom Actions 编辑器的Commit节中

Custom Action代码示例如下:

Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel

<RunInstaller(True)> _
Public Class CustomActionManager
    Inherits Installer
    Private Const Service_Name As String = "MyService"

    Public Sub New()
        MyBase.New()

        AddHandler Committed, AddressOf CustomActionManager_Committed
    End Sub

    Private Sub CustomActionManager_Committed(ByVal sender As Object, ByVal e As InstallEventArgs)
      
        'need to put this custom action under the Commit section of Setup -> Custom Actions wizard
        Dim svcController As New ServiceController(Service_Name)
        Try
            svcController.Start()
        Catch ex As Exception
            MsgBox("failed to start service " + Service_Name)
        Finally
            svcController.Dispose()
        End Try
    End Sub

End Class

原创粉丝点击