Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Debugging Windows Services in the Visual Studio IDE

In one of our current projects we made  the decision to move our WCF services from an IIS hosted environment into a Window Service hosted environment.  This move gave us greater flexibility in mangement and distribution of our WCF services, and allowed us to use multiple endpoints (http, net.tcp, pipes) without the need for Windows Activation Services (WAS) which would have required that our clients be running Vista and/or Server 2008.

One of the major pains about working with developing Windows Services, however, is that you can’t start a windows service project from within the debugger.  You have to instead install and start the service to execute it.

I have a workaround for this that allows us to test our WCF services from within the IDE without installing the service.

Steps:

1. In the Windows Service, put any code you’d normally put in the OnStart and OnStop events into their own functions:

private void OpenWCFServices()
{
    householdHost.Open();
}

private void CloseWCFServices()
{
    householdHost.Close();

}
protected override void OnStart(string[] args)
{
    OpenWCFServices();
}

protected override void OnStop()
{
    CloseWCFServices();
}

 

2. Using precompiler directives, specify 2 public functions that also call your start and stop methods.  This allows these methods to be publicly available at debug time only (you’ll see why in Step 3).

#if DEBUG
        public void DebugStart()
        {
            OpenWCFServices();
        }

        public void DebugStop()
        {
            CloseWCFServices();
        }
#endif

3. Create a new Windows Form in your service project.  Alter the constructor so that it requires an instance of your service to be created.  Add a Button to stop your WCF services to the form.

    public partial class DebugForm : Form
    {
        private FEEAApplicationServiceHost service;
        public DebugForm(FEEAApplicationServiceHost service)
        {
            InitializeComponent();
            this.service = service;
            this.service.DebugStart();
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
#if DEBUG
            service.DebugStop();
#endif
        }
    }

 

4. Alter your Program.cs to use this form as your running application instead of the Service using the same #if DEBUG directives.

       /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {

#if DEBUG
            Application.EnableVisualStyles();
            Application.Run(new DebugForm(new FEEAApplicationServiceHost()));
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new FEEAApplicationServiceHost()
            };
            ServiceBase.Run(ServicesToRun);
#endif

        }
    }

This allows you to launch your service from the IDE and debug during development, while still having the ability to compile and distribute your service without changing a line of code.  (Although I’d probably recommend removing this code from anything that is truly production-level.

So now, when you right click your service project and select Debug –gt; Start new instance, you see the following form:

 

image

Once this is up, you can start an instance of your code that accesses the service, and you’re all set.

1 Comment. Leave new

whats FEEAApplicationServiceHost ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\