Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Windows Service Application without installing it

Whem I'm writing a Windows Service and just hit F5 I get the error message that I have to install it using installutil.exe and then run it. In practice this means everytime I change a line of code:

  1. compile
  2. switch to Developer Command Prompt
  3. remove old version
  4. install new version
  5. start service

That is very inconvenient. Is there a better way to do it?

like image 251
Philippe Avatar asked May 17 '13 08:05

Philippe


People also ask

How do I run a Windows Service app?

Start and run the serviceIn Windows, open the Services desktop app. Press Windows+R to open the Run box, enter services. msc, and then press Enter or select OK.

Can a Windows Service launch an application?

Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop.


2 Answers

You can write this code in program.cs

//if not in Debug
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
   new MyService() 
};
ServiceBase.Run(ServicesToRun);

//if debug mode
MyService service = new MyService();
service.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

in MyService class

public void OnDebug()
{
   OnStart(null);
}


like image 60
Shital Kumbhar Avatar answered Sep 19 '22 18:09

Shital Kumbhar


The best way in my opinion is to use Debug directive. Below is an example for 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

Hit F5 And set a Breakpoint on your YourMethodContainingLogic Method to debug it.

like image 44
Mayank Pathak Avatar answered Sep 18 '22 18:09

Mayank Pathak