且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建Windows服务的多个实例?

更新时间:2023-11-22 16:43:58

服务安装实际上只是在名称,可执行文件和其他一些元数据之间添加映射。安装后,一项服务正在运行(一次)或没有运行。要使多个实例作为服务运行,它们必须使用不同的名称注册,这基本上意味着要多次安装。但这不是 ,这意味着您需要可执行文件的多个不同副本。

A service "installation" is really just adding a mapping between a name, an executable, and a few other bits of metadata. A service, once installed, is either running (once), or it isn't. To have multiple instances running as services, they would have to be registered with different names, which basically means installing it multiple times. That does not, however, mean that you need multiple different copies of the executable.

我这样做的方法是将安装程序,以便创建 ServiceInstaller ServiceProcessInstaller ,并给出 ServiceName (在运行时,通过自定义安装程序)到 ServiceInstaller 实例,然后使用 ManagedInstallerClass 在运行时-基本上使独立的exe能够使用命令行参数指定名称来作为服务安装和卸载自身(次数不限)。

The way I do it is to subclass Installer such that it creates a ServiceInstaller and ServiceProcessInstaller, and gives a ServiceName (at runtime, via the custom installer) to the ServiceInstaller instance, then use ManagedInstallerClass at runtime - basically making a standalone exe capable of installing and uninstalling itself as a service (as many times as you like), using command-line parameters to specify the name.