Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
17 Jul 2008 1  
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:

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:

    ' 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 allmost every functionality of Visual Studio through this object:

    ' Visual Studio Object.
    Private mDTE As EnvDTE.DTE

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

     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:

        ' 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:

        ' 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
End Sub 

In the Disposed event of the form you can enter similiar code to stop the windows service when you close the form:

     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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here