Starting the Windows Service application without installing it

Whem I write a Windows service and just press F5. I get an error message that I need to install using installutil.exe , and then run. In practice, this means that every time I change the line of code:

  • compilation
  • go to developer command line
  • delete old version
  • install new version
  • service start

It is very uncomfortable. Is there a better way to do this?

+7
source share
4 answers

I usually put the bulk of the service implementation in the class library, and then create two "front ends" to start it - one service project, the other a console or window application. I am using console / forms application for debugging.

However, you should be aware of the differences in the environment between the debugging experience and when starting up as a genuine service - for example, you may happen to be dependent on working in a session with an interactive user or (for winforms) where the message pump works.

+5
source

The best way, in my opinion, is to use the Debug directive. Below is an example of the same.

 #if(!DEBUG) ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { // Calling MyService Constructor new MyService() }; ServiceBase.Run(ServicesToRun); #else MyService serviceCall = new MyService(); serviceCall.YourMethodContainingLogic(); #endif 

Press F5 and set Breakpoint in your YourMethodContainingLogic method to debug it.

+7
source

You cannot start the Windows service, as another console or WinForms application says. It should be launched by Windows itself.

If you don’t have an infrastructure ready to use, as @Damien_The_Unbeliever offers (this is what I recommend), you can install this service from a debug location. Therefore, you use installutil once and point it to the executable located in /bin/debug . Then you start the service from services.msc and use the menu Visual Studio > Debug > Attach to Process and join the Windows service.

You can also use Thread.Sleep(10000) as the first line in an OnStart or Debugger.Break() call to help you connect before the service does any work. Remember to remove them before release.

+1
source

You can use the Environment.UserInteractive variable. Implementation details here

0
source

All Articles