Click here to Skip to main content
6,306,412 members and growing! (17,362 online)
Email Password   helpLost your password?
General Programming » Programming Tips » Design and Strategy     Beginner License: The Code Project Open License (CPOL)

Debugging Windows Service Without Deploying It

By Eric De Carufel

This article decribes an easy way to debug Windows Services without having to deploy it.
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Dev
Posted:5 Dec 2007
Updated:10 Dec 2007
Views:12,658
Bookmarked:35 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 3.61 Rating: 4.00 out of 5

1

2
1 vote, 12.5%
3
3 votes, 37.5%
4
4 votes, 50.0%
5
Screenshot - EasyDebugWindowsServices.JPG

Introduction

At the beginning, there was no easy way to build a Windows service using Visual Studio and .NET Framework. Now that Microsoft has integrated this functionality as a template and a class library, it's much more easy to do that. This is good, but to test your newly created service you have to deploy it. That means you must add an installer and build a whole MSI file and run it in your environment. Once deployed, you can start it with Windows Service controller and attach a debugger to it.

I don't know how this sounds to you, but to me it is too much unneeded work to do a quick test. This article describes a much more easy way to do that.

Using the Code

You will find a sample application using this concept. Here is how it works.

First you need a working Windows service. (For more information, refer to this.)

In Visual Studio, building a Web service is easy. Create a new project and select Windows Service template.

Then add ServiceDebuggerHelper project to your solution and a reference to it. You can find it in the sample code.

By default, when you create a Windows service project, Visual Studio will create two classes: a Program class and a Service1 class. Here is what the program class looks like:

static class Program 
{ 
    /// <summary>
    /// The main entry point for the application. 
    /// </summary>
    static void Main() 
    { 
        ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() }; 
        ServiceBase.Run(ServicesToRun); 
    } 
}

The first thing we have to do is to surround that code with a conditional statement to let us control if we want to run as a real service or just in debugging mode.

 static class Program 
{ 
    /// <summary>
    /// The main entry point for the application. 
    /// </summary>
    static void Main() 
    { 
        if (args.Length > 0 && args[0].ToLower().Equals("/debug")) 
        { 
            Application.Run(new ServiceRunner(new Service1())); 
        } 
        else 
        {        
            ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() }; 
            ServiceBase.Run(ServicesToRun); 
        }
    } 
}

In this new Program class, ServiceRunner will be acting as a service host. To make it work, you have to add the "/debug" command line option in your project properties.

Go to Project -> ... Properties -> Debug and type /debug in the command line arguments field.

From there, you have two options to make your service debuggable. Either you implement IDebuggableService or inherit from DebuggableService base class.

Implementing IDebuggableService Interface

If you already subclass ServiceBase and use that subclass for all your services or if you just want to have full control over the implementation, you should implement the IDebuggableService interface. Here is its definition:

public interface IDebuggableService
{
    void Start(string[] args);
    void StopService();
    void Pause();
    void Continue();
}

Notice that the stop method is called StopService to avoid conflict with the existing Stop method of the ServiceBase class.

Here is a sample implementation of this interface:

public partial class Service1 : ServiceBase, IDebuggableService        
{
    //
    // Your existing service code
    //
    // ...
    //
    
    #region IDebuggableService Members

    public void Start(string[] args)
    {
        OnStart(args);
    }

    public void StopService()
    {
        OnStop();
    }

    public void Pause()
    {
        OnPause();
    }

    public void Continue()
    {
        OnContinue();
    }

    #endregion
}

Subclassing DebuggableService

Subclassing your service from DebuggableService is even more simple. All you have to do is change the base class from ServiceBase to DebuggableService.

public partial class Service1 : DebuggableService        
{
    //
    // Your existing service code
    //
    // ...
    //
}

All the control methods are already implemented by DebuggableService.

Points of Interest

In my sample code, I added a Windows Form class to let you start, stop and pause your service. You can use it as is, build your own controller interface or just call the methods in your program class. It is up to you.

Of course you should still deploy your service to give it the final test run. One good reason to do this is to test the actual security context in which your service will run. It will not have the same permission as when you run it in Visual Studio, but this technique can speed up the development process.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Eric De Carufel


Member
Eric De Carufel is a .NET solution architect for Orckestra in Montreal. He is a specialist in application development with Miscrodoft .NET framwork. He has more than fifteen years of experience in software design in various domain as transportation, food store, insurance, engineering, finance and electronic business. Eric is memeber of Groupe d'usagers Visual Studio .NET de Montreal at which he gave some presentation. Eric is a Microsoft certified developer (Microsoft Certified Application Developer - MCAD)
Occupation: Architect
Company: Orckestra
Location: Canada Canada

Other popular Programming Tips articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralGreat solution Pinmembermishenkovks4:43 21 Feb '08  
GeneralRe: Great solution PinmemberJ0J0_12:06 28 Jun '08  
Generalminor typo [modified] PinmemberTobias Manthey6:56 22 Jan '08  
GeneralIDebuggableService Documentation PinmemberJeffrey Walton2:55 7 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Dec 2007
Editor: Deeksha Shenoy
Copyright 2007 by Eric De Carufel
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project