Debug Windows Service

后端 未结 11 1393
既然无缘
既然无缘 2020-11-29 17:09

Scenario

I\'ve got a windows service written in C#. I\'ve read all the google threads on how to debug it, but I still can\'t get it to work. I\'ve run \"PathTo.Net

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 17:50

    I recently added this to a project and it works great for me. You can debug it just like any other EXE. After it is added go to your project properties and add a command line parameter (/EXE) in the Debug tab for the Debug build configuration.

     _
    Shared Sub Main()
    
        '' 
        '' let's add a command line parameter so we can run this as a regular exe or as a service
        ''
        If Command().ToUpper() = "/EXE" Then
            Dim app As MyService = New MyService()
    
            app.OnStart(Nothing)
            Application.Run()
        Else
            Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    
            ' More than one NT Service may run within the same process. To add
            ' another service to this process, change the following line to
            ' create a second service object. For example,
            '
            '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
            '
            ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService}
    
            System.ServiceProcess.ServiceBase.Run(ServicesToRun)
        End If
    End Sub
    

提交回复
热议问题