A little trick i’ve used for quite some time…
 
If you want to test a windows service for instance, you can’t start it in the debugger. But I want to use the debugger.
 
Then you could write something like the code below in the Program.cs.
Insert an internal Start method in the service code that just calls the OnStart() method of your service.
 
Now for the magic… i can now easily switch the code blocks (first 2 lines and last 5 lines) by removing one of the first 2 asterix (*) or reinserting one.
 

        static void Main()

        {

/**/

            var service = new AgentService();

            service.Start();

/*/

            ServiceBase[] ServicesToRun;

            ServicesToRun = new ServiceBase[]

            {

                new AgentService()

            };

            ServiceBase.Run(ServicesToRun);

/**/

        }

 

Would become

 

        static void Main()

        {

/*/

            var service = new AgentService();

            service.Start();

/*/

            ServiceBase[] ServicesToRun;

            ServicesToRun = new ServiceBase[]

            {

                new AgentService()

            };

            ServiceBase.Run(ServicesToRun);

/**/

        }

 

 

 

Advertisements