使用 InstallUtil 安装 .NET 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:41:28  来源:igfitidea点击:

Installing a .NET service using InstallUtil

.netwindows-servicesinstallutil

提问by Karim

I'm trying to install a .NET service I wrote. As recommended by MSDN, I'm using InstallUtil. But I have missed how I can set the default service user on the command-line or even in the service itself. Now, when InstallUtil is run, it will display a dialog asking the user for the credentials for a user. I'm trying to integrate the service installation into a larger install and need the service installation to remain silent.

我正在尝试安装我编写的 .NET 服务。按照 MSDN 的建议,我正在使用 InstallUtil。但是我错过了如何在命令行甚至服务本身中设置默认服务用户。现在,当 InstallUtil 运行时,它将显示一个对话框,要求用户提供用户的凭据。我正在尝试将服务安装集成到更大的安装中,并且需要服务安装保持静默。

回答by Karim

I think I may have found it. In the service itself, the automatically created ServiceProcessInstaller component has a property "Account" which can be set to "LocalService", "LocalSystem", "NetworkService" or "User". It was defaulting to "User" which must have displayed the prompt.

我想我可能已经找到了。在服务本身中,自动创建的 ServiceProcessInstaller 组件有一个属性“Account”,它可以设置为“LocalService”、“LocalSystem”、“NetworkService”或“User”。它默认为“用户”,它必须显示提示。

回答by Aoi Karasu

As you noticed, Karim, "Account" property is the solution, here. For those interested in differences between security contexts set by this property:

正如您所注意到的,Karim 的“帐户”属性是这里的解决方案。对于那些对此属性设置的安全上下文之间的差异感兴趣的人:

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx

Above using InstallUtil or SC, I like the idea of creating a SELF INSTALLER:

以上使用 InstallUtil 或 SC,我喜欢创建SELF INSTALLER的想法:

http://www.codeproject.com/KB/dotnet/WinSvcSelfInstaller.aspx

http://www.codeproject.com/KB/dotnet/WinSvcSelfInstaller.aspx

even though I found this in the .Net 1.1 documentation:

即使我在 .Net 1.1 文档中发现了这一点:

The ManagedInstallerClasstype supports the .NET Framework infrastructure and is not intended to be used directly from your code.

ManagedInstallerClass类型支持.NET Framework基础结构,不适合直接在代码中使用。

回答by Craig Tyler

Also keep in mind the SC.exe utilwhich does not require visual studio to be installed. You can simply copy this exe to the server you want to create the service or even run it remotely. Use the objparameter to specify a user.

还要记住不需要安装 Visual Studio的SC.exe 实用程序。您可以简单地将此 exe 复制到要创建服务的服务器,甚至可以远程运行它。使用obj参数指定用户。

Apparently there is a GUI for this tool, but I have not used it.

显然这个工具有一个 GUI,但我没有使用它。

回答by Craig Tyler

Are you being asked for the account to run the service under, or for rights to install the service? For the second, installing as admin should prevent that from happening. For the first, you have to add a ServiceProcessInstaller to your Installer.

是否要求您提供运行该服务的帐户,或安装该服务的权限?其次,以管理员身份安装应该可以防止这种情况发生。首先,您必须向安装程序添加 ServiceProcessInstaller。

I believe the design surface for a service has a link to create a Project Installer. On that designer, you can add a process installer of type System.ServiceProcess.ServiceProcessInstaller. The properties of this object allow you to set the account to use for the service.

我相信服务的设计图面有一个链接来创建一个项目安装程序。在该设计器上,您可以添加 System.ServiceProcess.ServiceProcessInstaller 类型的进程安装程序。此对象的属性允许您设置用于服务的帐户。

回答by D. Lockett

InstallUtil has command line switches which can avoid the prompts when using "User" as the account type. /usernameand /passwordare used configure the account at install time.

InstallUtil 具有命令行开关,可以避免在使用“用户”作为帐户类型时出现提示。/username/password用于在安装时配置帐户。

Usage:

用法:

installutil.exe /username=user /password=password yourservice.exe

What you may want is to have a config file where the installer can read and install the service.

您可能想要的是有一个配置文件,安装程序可以在其中读取和安装服务。

To do so, add a service installer to your project, and overload the install method. In this method, set the username and password:

为此,请将服务安装程序添加到您的项目中,并重载 install 方法。在这个方法中,设置用户名和密码:

public override void Install(IDictionary stateSaver)
{
    serviceProcessInstaller1.Username="<username>";
    serviceProcessInstaller1.Password="<password>";
    base.Install(stateSaver);
}

If you try to set the username and password in the constructor, those values will be overwritten, so make sure you override "Install" to do so.

如果您尝试在构造函数中设置用户名和密码,这些值将被覆盖,因此请确保您覆盖“安装”以执行此操作。