How to create a Windows service, which is a standalone exe application?

I would like to create a console exe application that can run both a standalone application and a Windows service. Can this be done? What are the benefits of using svchost?

+3
source share
2 answers

Rewrite the main method of the Windows service like this, and it will be a console application if you run with the -c option, and yes, remember to change the project type for the console from the project properties window.

 public static void Main(string[] args)
 {
      Service service = new Service();
      if (args.Contains("-c", StringComparer.InvariantCultureIgnoreCase) || args.Contains("-console", StringComparer.InvariantCultureIgnoreCase))
      {
           service.StartService(args);
           Console.ReadLine();
           service.StopService();
           return;
      }
      ServiceBase[] ServicesToRun = new ServiceBase[] 
                                    { 
                                        service 
                                    };
      ServiceBase.Run(ServicesToRun);           
  }

StartService and StopService simply cause the OnStart and OnStop services to be redefined

+14
source

The best approach to these cases is to think about the concept of a component.

DLL .

Windows, DLL.

.

, DLL . - .

+2

Source: https://habr.com/ru/post/1712848/


All Articles