Debugging a Windows Service

Now that your service is installed and running, you can use the Visual Studio .NET debugger to attach to the service and use the standard debugging tools, such as, breakpoints, stepping through code and others, to make sure your service is running correctly.

In Exercise 1.3, you will be attaching the debugger to a Windows service. You will be using a special capability of the Visual Studio .NET debugger that enables you to attach the debugger to an external process running on the computer. Because you have access to the source code for your service, you can set breakpoints. While the service is running, when a breakpoint is hit, you will go into break mode and can step through the code to examine variable values and perform other debugging actions.

Exercise 1.3: Debugging a Windows Service

start example
  1. In Visual Studio .NET, open the CustomLogService project. Right-click CustomLogService.vb in the Solution Explorer and choose View Code.

  2. Set a breakpoint on the line of code in the OnStop procedure that writes the log entry:

    CustomEventLog.WriteEntry("The service has been stopped.")
  3. From the Visual Studio .NET menus, choose Debug Ø Processes. You will see a list of running processes on your computer. Make sure that the check boxes labeled Show System Processes and Show Processes in all Sessions are both selected.

    click to expand

  4. Select CustomLogService and click the Attach button.

  5. The Attach to Process dialog box displays. Make sure that the Common Language Runtime option is checked and click OK. Close the Processes dialog box.

  6. Start the Service Control Manager. Select CustomLogService and stop the service. A yellow highlight in Visual Studio .NET indicates that the breakpoint has been hit. The Service Control Manager will not be able to finish stopping the service until you release the debugger. Choose Debug Ø Stop Debugging from the menu to do so.

end example

Warning 

Be careful when using the debugger to attach to a process. Use this technique only when you are working with processes that you can control. Attaching a debugger to one of the operating system processes, for example, could cause your computer to hang up.



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net