Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / Visual Basic

How to Start and Debug a Windows Service with Visual Studio 2008

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
17 Jul 2008LGPL32 min read 54.2K   173   27   13
This article demonstrates how to start and debug a Windows service with Visual Studio 2008.

Introduction

This article demonstrates how to start and debug a Windows service with Visual Studio 2008. It will also work with Visual Studio 2005.

Background

When developing a Windows service with Visual Studio, you usually have to start the Windows service with the Service Control Manager, and then you have to attach the debugger to the process. But this is not necessary. You can automate this process in most cases so you only have to press F5.

Using the Code

Visual Studio can be automated just like Office. You can use this feature to start and debug a Windows service by pressing F5.

To achieve this goal, create a new Windows Forms project in the solution of your Windows Service project and make it the startup project of your solution. Name the project ServiceDebug and name the form console.

Make some Imports to the class of the form:

VB
Imports System.ServiceProcess
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports EnvDTE

This will make the code more readable.

There are two constants. One contains the name of the Windows service, the other contains the full path to the executable of the Windows service:

VB
' Replace the value of this constant with the name of the service.
Private mcsServiceName As String = "INSERT_SERVICE_NAME_HERE"
' Replace the value of this constant with the path of the executable.
Private mcsExecutablePath As String = "INSERT_EXECUTABLE_PATH_HERE"

There is one private variable in the class for the Visual Studio automation object, DTE. It is comparable to Office's Application object; you can access almost every functionality of Visual Studio through this object:

VB
' Visual Studio Object.
Private mDTE As EnvDTE.DTE

All the work will be done in the Load event of the form:

VB
Private Sub Console_Load(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
' Array with all installed windows services.
Dim aoServices() As ServiceController

Dim bFoundService As Boolean
Dim bFoundProcess As Boolean

The first task is to find the Windows service and to start it. You can use the ServiceController class to do this:

VB
' Search the service.
aoServices = ServiceController.GetServices()

' Loop through all services.
For Each oService As ServiceController In aoServices
    ' Service found?
    If (oService.ServiceName = mcsServiceName) Then
        ' Service found. Set flag.
        bFoundService = True

        ' Check state.
        Select Case oService.Status
            ' Running?
            Case ServiceControllerStatus.Running
                ' Stopped?
            Case ServiceControllerStatus.Stopped
                ' Start it.
                oService.Start()
                ' Paused?
            Case ServiceControllerStatus.Paused
                ' Start it.
                oService.Start()
                ' Something else.
            Case Else
                Throw New Exception("Der Windows-Dienst " + mcsServiceName _
                                  + " kann nicht gestartet werden.")
        End Select
    End If
Next oService

' Raise exception if service not found.
If Not bFoundService Then
    Throw New Exception("Der Windows-Dienst " + mcsServiceName _
                      + " ist nicht installiert.")
End If

The second task is to find the process and to attach the Visual Studio debugger to it. You can use the Debugger property of the DTE object to do this:

VB
' Get Visual Studio-Object.
mDTE = CType(Marshal.GetActiveObject("VisualStudio.DTE"), EnvDTE.DTE)

' Search the process.
For Each oEnvDTEProcess As EnvDTE.Process In mDTE.Debugger.LocalProcesses
    Try
        ' Process found?
        If (oEnvDTEProcess.Name = mcsExecutablePath) Then
            ' Process found.
            bFoundProcess = True

            ' Attach to the process.
            oEnvDTEProcess.Attach()
        End If
    Catch ex As Win32Exception
        Debug.Print(ex.ToString)
    Catch ex As COMException
        Debug.Print(ex.ToString)
    End Try
Next oEnvDTEProcess

' Raise exception if process not found.
If Not bFoundProcess Then
    Throw New Exception("Der Prozess " + mcsExecutablePath _
                      + " existiert nicht.")
End If

In the Disposed event of the form, you can enter similar code to stop the Windows service when you close the form:

VB
Private Sub Console_Disposed(ByVal sender As Object, _
                             ByVal e As System.EventArgs) Handles Me.Disposed
    ' Array with all installed windows services.
    Dim aoServices() As ServiceController

    ' Flag set if the service was found.
    Dim bFoundService As Boolean

    ' Search the service.
    aoServices = ServiceController.GetServices()

    ' Loop through all services.
    For Each oService As ServiceController In aoServices
        ' Service found?
        If (oService.ServiceName = mcsServiceName) Then
            ' Service found. Set flag.
            bFoundService = True

            ' Check state.
            Select Case oService.Status
                ' Running?
                Case ServiceControllerStatus.Running
                    ' Stop it.
                    oService.Stop()
                    ' Stopped?
                Case ServiceControllerStatus.Stopped
                    ' Paused?
                Case ServiceControllerStatus.Paused
                    ' Stop it.
                    oService.Stop()
                    ' Something else.
                Case Else
                    Throw New Exception("Der Windows-Dienst " + mcsServiceName _
                                      + " kann nicht gestoppt werden.")
            End Select
        End If
    Next oService

    ' Raise exception if service not found.
    If Not bFoundService Then
        Throw New Exception("Der Windows-Dienst " + mcsServiceName _
                          + " ist nicht installiert.")
    End If
End Sub

Points of Interest

This project will, in most cases, reduce the effort to start and debug a Windows service with Visual Studio to pressing F5. There is one important limitation. When debugging the startup of a Windows service, you cannot use this technique. The reason for this is not clear to me. It just looks like the constructor of the Windows service has already finished when the debugger attaches to the process. If someone has an idea, please let me know.

History

  • 2008.07.18: First try.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNeed Help... Pin
Jason Langley30-Nov-08 2:29
Jason Langley30-Nov-08 2:29 
AnswerRe: Need Help... Pin
Member 411560422-Jun-09 14:45
Member 411560422-Jun-09 14:45 
General.NET API to debug code Pin
Member 430703520-Jul-08 23:06
Member 430703520-Jul-08 23:06 
Generaleasy way Pin
Pauloc19-Jul-08 5:56
Pauloc19-Jul-08 5:56 
GeneralInstaller/Uninstaller Pin
Happy camper18-Jul-08 8:41
Happy camper18-Jul-08 8:41 
GeneralRe: Installer/Uninstaller Pin
Michael Leithold21-Jul-08 3:43
Michael Leithold21-Jul-08 3:43 
GeneralAlternative method Pin
jimbobmcgee18-Jul-08 1:30
jimbobmcgee18-Jul-08 1:30 
GeneralRe: Alternative method Pin
PHenry18-Jul-08 7:25
PHenry18-Jul-08 7:25 
GeneralRe: Alternative method Pin
Happy camper18-Jul-08 7:49
Happy camper18-Jul-08 7:49 
GeneralRe: Alternative method Pin
jimbobmcgee20-Jul-08 23:27
jimbobmcgee20-Jul-08 23:27 
GeneralRe: Alternative method Pin
Michael Leithold21-Jul-08 3:39
Michael Leithold21-Jul-08 3:39 
GeneralRe: Alternative method Pin
Michael Leithold21-Jul-08 3:35
Michael Leithold21-Jul-08 3:35 
GeneralRe: Alternative method Pin
Peter Mauger5-Aug-08 16:14
Peter Mauger5-Aug-08 16:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.