Dowemo
0 0 0 0


Question:

Is it possible to debug the Windows services in Visual Studio?

I used code like

System.Diagnostics.Debugger.Break();

but it is giving some code error like

I got two event error: eventID 4096 VsJITDebugger and "The service did not respond to the start or control request in a timely fashion. "


Best Answer:


I'm using a /Console parameter in the visual studio project Debug> Start Options> Command line arguments:

public static class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
         var runMode = args.Contains(@"/Console")
             ? WindowsService.RunMode.Console 
             : WindowsService.RunMode.WindowsService;
         new WinodwsService().Run(runMode);
    }
}
public class WindowsService : ServiceBase
{
    public enum RunMode
    {
        Console,
        WindowsService
    }
    public void Run(RunMode runMode)
    {
        if (runMode.Equals(RunMode.Console))
        {
            this.StartService();
            Console.WriteLine("Press <ENTER> to stop service...");
            Console.ReadLine();
            this.StopService();
            Console.WriteLine("Press <ENTER> to exit.");
            Console.ReadLine();
        }
        else if (runMode.Equals(RunMode.WindowsService))
        {
            ServiceBase.Run(new[] { this });
        }
    }
    protected override void OnStart(string[] args)
    {
        StartService(args);
    }
    protected override void OnStop()
    {
        StopService();
    }
    /// <summary>
    /// Logic to Start Service
    /// Public accessibility for running as a console application in Visual Studio debugging experience
    /// </summary>
    public virtual void StartService(params string[] args){ ... }
    /// <summary>
    /// Logic to Stop Service
    /// Public accessibility for running as a console application in Visual Studio debugging experience
    /// </summary>
    public virtual void StopService() {....}
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs