C#,vb.net,MVC,Jquery,javascript,jscript,vbscript,html,vb,sharepoint,COM,WPF,WCF,Wwf,Asp,Asp.net,questions & answers,

Latest in Sports

Thursday, September 26, 2019

How to debug Windows Service as a console application in .Net?.

How to debug Windows Service as a console application?.

Step 1: Add a public RunAsConsole method to your service class.

        public void RunAsConsole(string[] args)
        {
            OnStart(args);
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
            OnStop();
        }

Step 2: Opt out of the ServiceBase when running as a console

Change Program.cs main method so it opts out of full service mode when running in interactive mode (i.e. as a console), while falling back to normal service behaviour when run by the Service Controller:
 static class Program
    {
        static void Main(string[] args)
        {
            MyService service = new MyService();
            if (Environment.UserInteractive)
            {
                service.RunAsConsole(args);
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { service };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }

No comments:

Post a Comment