Setting Up Debug Mode for a Windows Service Project

January 14, 2011 by Ben Galluzzo    .NET, Debugging, Windows Service, C# |    Comments (0)

If you’ve tried to run a Windows Service project in debug mode, you’ve probably seen the message shown below.  To debug our code by default, we would have to install the service and then attach the debugger to a Process (Ctrl+Alt+P).  Obviously, this can get tedious.  There is a more appropriate and efficient way to debug the Windows Service project in which we’re developing.

DebugModeError_489x199

 

 

 

 

 

 

Let’s start out by constructing a simple Windows Service…  Add a Windows Service class item to the project; in our case, the class is named AgentService

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.Timers;

namespace ProvingGrounds.Agent
{
    partial class AgentService : ServiceBase
    {
        private Timer timer = null;

        public AgentService()
        {
            InitializeComponent();

            timer = new Timer(6000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            timer.AutoReset = true;
        }

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

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

        public void Start()
        {
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //TODO:Do Something
        }
    }
}

 

Next, update Main() found in Program.cs to execute AgentService.

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AgentService() 
			};
            ServiceBase.Run(ServicesToRun);
        }

We should now have our Windows Service constructed. Feel free to compile it and then go ahead try running it in debug mode. You should be receiving our "Start Failure" error message.

Next, let's get this thing running in debug mode from Visual Studio. We can do this by adding the #if DEBUG preprocessor directive to our Main() function. We simply wrap the existing call to our AgentService with the conditional preprocessor directives.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.Threading;

namespace ProvingGrounds.Agent
{
    static class Program
    {
        static void Main()
        {
            #if DEBUG
            
            AgentService agentService = new AgentService();
            agentService.Start();

            Thread.Sleep(Timeout.Infinite);

            agentService.Stop();

            #else

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AgentService() 
			};
            ServiceBase.Run(ServicesToRun);
            
            #endif
        }
    }
}

 

That's all there is to.  Go ahead and hit F5 from Visual Studio and your Windows Project should be running in Debug Mode.


SQL Saturday - 506 - Baltimore BI Edition

Month List