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

Developing Windows Services using Visual Studio .NET Explained - Part 1

0.00/5 (No votes)
25 Sep 2008 1  
An overview and step by step guide to developing, using and debugging a windows service

Introduction

One of the most difficult task for a entry to mid-level .NET programmer is debugging a .NET Windows Service while it is live. Even if you do have a development server that you can use, debugging existing code may seem a mysterious task at first. I remember a few years back when I was researching this topic on the web, no one gives you a straight and simple answer about this topic. So here is my attempt at simplifying this topic a bit.

Windows Service Architecture

  1. Service Application: This type application is basically the actual Windows .NET Service that runs under Services.
  2. Service Controller Application: This type of application enables you to control the behavior of an existing application, start, stop, pause and continue a service.
  3. Service Control Manager: This is a utility that comes with Windows that enables you to control all services that are installed on a computer. It can be found at "Control Panel > Administrative Tools > Services".

Namespaces

  • Windows Service: System.ServiceProcess
  • Installer Namespaces: System.ServiceProcess.ServiceInstaller and ServiceProcessInstaller
  • Service Controller: System.ServiceProcess.ServiceController

    Registry Information

    The registry location that contains a list of all services running on a Windows Machine is:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

    Here is how a typical registry entry looks when a .NET Service is installed:


    Screenshot - 081507_b1_i1.gif

    The most notable one is the "ImagePath" entry, which contains the path to the executable that runs the service. This value can be change to point to different versions of the service executable for testing purposes.

    Important Notes

  • Services HAVE TO be installed before running them the executable, in order to use them. You can NOT simply press RUN in Visual Studio and run them that way, or double click on the executable and expect to interact with them!
  • You can NOT debug Windows Services using F5, F10 or F11 stepping through code method. In order to be able to do that, the service has to be installed, started under Windows Service Controller, and its process HAS to be attached to its source code in Visual Studio.
  • Any Message Boxes, Dialog Boxes, or Windows Forms opened or used by a Windows Service are HIDDEN from the user, unless you check the checkbox under Service Properties that says "Interact with Desktop", in which case you will see them all!
  • Error Messages that occur in a Windows Service are logged in the Machine's "Event Log".

    Methods Available

    The following is a list of Methods that can / have to be overriden in order to run the service successfully or run a Service Controller Application:

    1. Methods of Service Base Class (to Override)
    • OnStart()
    • OnPause()
    • OnStop()
    • OnContinue()
    • OnShutDown()
    • OnCustomCommand()
    • OnPowerEvent()
    1. Methods of Service Controller Class
    • Close()
    • Continue()
    • ExecuteCommand()
    • Pause()
    • Refresh()
    • Start()
    • Stop()

    Click HERE to go to Part 2 of this article.

    Pete Soheil
    DigiOz Multimedia
    http://www.digioz.com/

  • 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