C# 安装 Windows 服务时的凭据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2253051/
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
Credentials when Installing Windows Service
提问by Keith
I am attempting to install a C# windows service project using a VisualStudio.Net deployment project.
我正在尝试使用 VisualStudio.Net 部署项目安装 C# windows 服务项目。
To run the deployment project I right-click and select "install" from the context menu, the install wizard runs and eventually prompts me with a "Set Service Login" dialog which asks for username & password.
要运行部署项目,我右键单击并从上下文菜单中选择“安装”,安装向导运行并最终提示我“设置服务登录”对话框,要求输入用户名和密码。
When I install a service using the sc utility from the command line, I don't have to provide credentials.
当我从命令行使用 sc 实用程序安装服务时,我不必提供凭据。
Do I have to create a login just for this service? I'd prefer to use "Local System" or "Network Service" (not sure what the difference is) as other services do.
我是否必须为此服务创建登录名?我更喜欢像其他服务一样使用“本地系统”或“网络服务”(不确定有什么区别)。
采纳答案by anthares
Add this code to your private void InitializeComponent()
method in projectInstaller.Designer.cs
file in your windows service project.
将此代码添加到Windows 服务项目文件中的私有 voidInitializeComponent()
方法中projectInstaller.Designer.cs
。
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
if the definition of you process installer is:
如果您的进程安装程序的定义是:
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
回答by fre0n
In the project that contains the service, add an Installer class. Make it look something like this:
在包含该服务的项目中,添加一个 Installer 类。让它看起来像这样:
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
public MyServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem; // Or whatever account you want
var serviceInstaller = new ServiceInstaller
{
DisplayName = "Insert the display name here",
StartType = ServiceStartMode.Automatic, // Or whatever startup type you want
Description = "Insert a description for your service here",
ServiceName = "Insert the service name here"
};
Installers.Add(_serviceProcessInstaller);
Installers.Add(serviceInstaller);
}
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
// This will automatically start your service upon completion of the installation.
try
{
var serviceController = new ServiceController("Insert the service name here");
serviceController.Start();
}
catch
{
MessageBox.Show(
"Insert a message stating that the service couldn't be started, and that the user will have to do it manually");
}
}
}
Then, in the solution explorer, right-click on the deployment project and select "View > Custom Actions". Right-click on Custom Actions, and select "Add Custom Action..." Pick the Application Folder and select the primary output of the project that contains the service. Now the custom actions (Commit
from above) will be executed upon installation. You can add the additional methods (Install
, Rollback
, Uninstall
) if you need other custom actions.
然后,在解决方案资源管理器中,右键单击部署项目并选择“查看 > 自定义操作”。右键单击自定义操作,然后选择“添加自定义操作...” 选择应用程序文件夹并选择包含该服务的项目的主要输出。现在自定义操作(Commit
来自上面)将在安装时执行。如果您需要其他自定义操作,您可以添加其他方法 ( Install
, Rollback
, Uninstall
)。
回答by Adam Cooper
- Open your ProjectInstaller
- Right click the ProcessInstaller and choose properties
- From the Account drop-down, under Misc, select the account you want your service to run as
- 打开您的项目安装程序
- 右键单击 ProcessInstaller 并选择属性
- 从“帐户”下拉列表中的“其他”下,选择您希望服务运行的帐户
For details of the different accounts and their privileges see the following link:
有关不同帐户及其权限的详细信息,请参阅以下链接:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx
回答by David
Check this Link: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
检查此链接:http: //msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
Pay attention to this section: To create the installers for your service
请注意此部分:为您的服务创建安装程序
Make changes to your ServiceProcessInstaller:
对您的 ServiceProcessInstaller 进行更改:
In the designer, click ServiceProcessInstaller1 for a Visual Basic project, or serviceProcessInstaller1 for a Visual C# project. Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.
在设计器中,为 Visual Basic 项目单击 ServiceProcessInstaller1,或为 Visual C# 项目单击 serviceProcessInstaller1。将帐户属性设置为本地系统。这将导致安装服务并在本地服务帐户上运行。