![]() |
Languages »
VB.NET »
General
Beginner
License: The GNU Lesser General Public License
How to Start and Debug a Windows Service with Visual Studio 2008By Michael LeitholdThis article demonstrates how to start and debug a windows service with Visual Studio 2008. |
VB (VB 9.0), Windows (WinXP), Visual Studio (VS2008), Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article demonstrates how to start and debug a windows service with Visual Studio 2008. It will also work with Visual Studio 2005.
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.
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.DTEAll 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
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.
2008.07.18: First try.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Jul 2008 Editor: |
Copyright 2008 by Michael Leithold Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |