When I use VS2010 SP1, I write a windows service. Now I want to debug it without installing it. So I write code in Program.cs main method as below:
#if (DEBUG)
ControllerNTService service =new ControllerNTService();
Console.ReadLine();
#else
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new ControllerNTService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#endif
I expected to debug the windows service in VS 2010. But in VS, the code lines below shows gray. It means the gray codes are invalid, am I right?
(the two lines are gray)
ControllerNTService service =new ControllerNTService();
Console.ReadLine();
If the codes are valid, I think I can run into them.
Another question, using code above, when I press F5 to debug it, it shows that it is not able to debug it, I need to install the service first.
I hope someone encountered similar issue to guide me.
Have a nice day
You should check the active Build configuration of your project. It needs to be set to “DEBUG”. (I think it is set to “RELEASE” by what you describe)
You can change the active Build configuration using Menu Build -> ConfigurationManager…-> In the Dialog set the Active Solution Configuratio to “DEBUG”.
If you want to start your service from command line you also need to start it. Therefore you should add a Start method to your ControllerNTService that calls the protected OnStart method on the instance
In your main method then you should call Start on the service instance.
In addition to starting it is also a good idea to provide a method to stop the service (which calls the procted method OnStop). This method is called after the Console.ReadLine to stop the service.