如何创建一个独立的exe应用程序的Windows服务?

时间:2022-06-11 20:45:52

I would like to create an console exe application, that may be run as a standalone application as well as a windows service. Is it possible to do? What are actually the benefits of using svchost?

我想创建一个控制台exe应用程序,它可以作为独立的应用程序以及Windows服务运行。有可能吗?使用svchost实际上有什么好处?

2 个解决方案

#1


Rewrite your windows service's main method like this, and it will be console application if you run with parametr -c, and yes don't forgot to change project type to console from project's property window

重写你的windows服务的主要方法,如果你使用parametr -c运行它将是控制台应用程序,是的,不要忘记从项目的属性窗口将项目类型更改为控制台

 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 just call service's overrided OnStart, and OnStop methodes

StartService和StopService只调用服务的覆盖OnStart和OnStop方法

#2


The best approach to this cases is to think about the notion of a component.

这种情况的最佳方法是考虑组件的概念。

Your strategy should be develop a DLL with all the logic you need.

您的策略应该是开发一个具有您需要的所有逻辑的DLL。

After that, you build a console application and windows service that use that DLL.

之后,您构建一个使用该DLL的控制台应用程序和Windows服务。

The notion of components and the possibility to reuse them is trivial and high advisable.

组件的概念和重用它们的可能性是微不足道的,并且是可取的。

When you need to change your logic, you only must change the DLL, and replace with the old one. You don't need any kind of special deploy.

当您需要更改逻辑时,您只需更改DLL,并替换旧的DLL。您不需要任何特殊部署。

#1


Rewrite your windows service's main method like this, and it will be console application if you run with parametr -c, and yes don't forgot to change project type to console from project's property window

重写你的windows服务的主要方法,如果你使用parametr -c运行它将是控制台应用程序,是的,不要忘记从项目的属性窗口将项目类型更改为控制台

 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 just call service's overrided OnStart, and OnStop methodes

StartService和StopService只调用服务的覆盖OnStart和OnStop方法

#2


The best approach to this cases is to think about the notion of a component.

这种情况的最佳方法是考虑组件的概念。

Your strategy should be develop a DLL with all the logic you need.

您的策略应该是开发一个具有您需要的所有逻辑的DLL。

After that, you build a console application and windows service that use that DLL.

之后,您构建一个使用该DLL的控制台应用程序和Windows服务。

The notion of components and the possibility to reuse them is trivial and high advisable.

组件的概念和重用它们的可能性是微不足道的,并且是可取的。

When you need to change your logic, you only must change the DLL, and replace with the old one. You don't need any kind of special deploy.

当您需要更改逻辑时,您只需更改DLL,并替换旧的DLL。您不需要任何特殊部署。