安装 .NET Windows 服务,无需 InstallUtil.exe
我有一个用 C# 编写的标准 .NET Windows 服务。
它可以在不使用InstallUtil的情况下自行安装吗? 我应该使用服务安装程序类吗? 我应该如何使用它?
我希望能够调用以下内容:
MyService.exe -install
它将与调用具有相同的效果:
InstallUtil MyService.exe
I have a standard .NET windows service written in C#.
Can it install itself without using InstallUtil?
Should I use the service installer class? How should I use it?
I want to be able to call the following:
MyService.exe -install
And it will have the same effect as calling:
InstallUtil MyService.exe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,这是完全可能的(即我正是这样做的); 您只需要引用正确的 dll (System.ServiceProcess.dll) 并添加安装程序类...
这是一个示例:
Yes, that is fully possible (i.e. I do exactly this); you just need to reference the right dll (System.ServiceProcess.dll) and add an installer class...
Here's an example:
查看 InstallHelper 方法ManagedInstaller 类。 您可以使用以下方法安装服务:
这正是 InstallUtil 的作用。 参数与 InstallUtil 的参数相同。
这种方法的好处是不会弄乱注册表,并且使用与 InstallUtil 相同的机制。
Take a look at the InstallHelper method of the ManagedInstaller class. You can install a service using:
This is exactly what InstallUtil does. The arguments are the same as for InstallUtil.
The benefits of this method are that it involves no messing in the registry, and it uses the same mechanism as InstallUtil.
尽管涉及的工作量并不小,但您始终可以回到旧的 WinAPI 调用。 不要求通过 .NET 感知机制安装 .NET 服务。
安装:
OpenSCManager
打开服务管理器。CreateService
来注册服务。ChangeServiceConfig2
来设置描述。CloseServiceHandle
关闭服务和服务管理器句柄。卸载:
OpenSCManager
打开服务管理器。OpenService
打开服务。OpenService
返回的句柄上调用DeleteService
来删除服务。CloseServiceHandle
关闭服务和服务管理器句柄。我更喜欢使用
ServiceInstaller
/ServiceProcessInstaller
的主要原因是您可以使用自己的自定义命令行参数注册服务。 例如,您可以将其注册为“MyApp.exe -service”
,然后如果用户在没有任何参数的情况下运行您的应用程序,您可以为他们提供一个 UI 来安装/删除服务。在
ServiceInstaller
上运行 Reflector 可以填补此简短说明中缺少的详细信息。PS 显然,这不会产生“与调用 InstallUtil MyService.exe 相同的效果” - 特别是,您将无法使用 InstallUtil 进行卸载。 但看来这对你来说也许并不是一个真正严格的要求。
You can always fall back to the good old WinAPI calls, although the amount of work involved is non-trivial. There is no requirement that .NET services be installed via a .NET-aware mechanism.
To install:
OpenSCManager
.CreateService
to register the service.ChangeServiceConfig2
to set a description.CloseServiceHandle
.To uninstall:
OpenSCManager
.OpenService
.DeleteService
on the handle returned byOpenService
.CloseServiceHandle
.The main reason I prefer this over using the
ServiceInstaller
/ServiceProcessInstaller
is that you can register the service with your own custom command line arguments. For example, you might register it as"MyApp.exe -service"
, then if the user runs your app without any arguments you could offer them a UI to install/remove the service.Running Reflector on
ServiceInstaller
can fill in the details missing from this brief explanation.P.S. Clearly this won't have "the same effect as calling: InstallUtil MyService.exe" - in particular, you won't be able to uninstall using InstallUtil. But it seems that perhaps this wasn't an actual stringent requirement for you.
这是我在编写服务时使用的一个类。 我通常会在未调用服务时出现一个交互式屏幕。 从那里我根据需要使用该类。 它允许同一台机器上有多个命名实例 - 因此 InstanceID 字段
示例调用
类本身
Here is a class I use when writing services. I usually have an interactive screen that comes up when the service is not called. From there I use the class as needed. It allows for multiple named instances on the same machine -hence the InstanceID field
Sample Call
The class itself
上面的例子对我来说并没有真正起作用,并且作为#1解决方案的论坛链接很难挖掘。 这是我编写的一个类(部分),另一部分是从 此链接合并的我发现埋在某处
要安装服务,请运行 InstallAndStart 命令,如下所示:
确保运行该程序的帐户有权安装服务。 您始终可以在程序上“以管理员身份运行”。
我还包含了几个用于非 api 访问的命令,这些命令不会安装或删除服务,但您可以列出它们并控制其中几个(启动、停止、重新启动)。 您实际上只需要提升安装或删除服务的权限。
还有一些用于获取和设置环境变量的命令,例如 OPENSSL_CONF 或 TEMP。 在大多数情况下,参数和方法名称应该是不言自明的。
The above examples didn't really work for me, and the link to the forum as a #1 solution is awful to dig through. Here is a class I wrote (in part), and the other bit is merged from this link I found buried somewhere
To install a service, run the InstallAndStart command as follows:
Make sure the account that is running the program has permission to install services. You can always 'Run As Administrator' on the program.
I have also included several commands for non-api access which do not install or remove services, but you can list them and control several (start, stop, restart). You really only need to elevate permissions for installing or removing services.
There are a couple of commands for getting and setting environment variables as well, such as
OPENSSL_CONF
orTEMP
. For the most part, the parameters and method names should be pretty self-explanatory.如果尝试将命令行应用程序安装为 Windows 服务,请尝试“NSSM”实用程序。 相关 ServerFault 详细信息可在此处找到。
In the case of trying to install a command line application as a Windows service try the 'NSSM' utility. Related ServerFault details found here.