Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Debugging windows services in Visual studio

Modified: 2008/01/30 10:40 by BHammendorp - Categorized as: Visual Studio
If you want to debug a windows service created in Visual Studio, all you have to do is change a piece of the code in the Program.cs:

namespace MyService
{
    static class Program
    {
        static void Main()
        {
#if DEBUG
            Service1 windowsService = new Service1();
            windowsService.Start();
            do
            {
                System.Threading.Thread.Sleep(1000);
            } while (true);
#else
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#endif
        }
    }
}

After you have changed this piece of code, you must open the Service1.cs and add a public method Start() and then change the OnStart(...) method to call the Start() method you just created.

At this point we have created a windows service capable of being ran (debugged) from Visual Studio without having to attach to a process.

 © Evident Interactive BV