安装Windows服务时的凭据

60

我正试图使用VisualStudio.Net部署项目安装一个C# Windows服务项目。

要运行部署项目,我右键单击并从上下文菜单中选择“安装”,然后安装向导运行,并最终提示我出现“设置服务登录”对话框,要求输入用户名和密码。

当我使用命令行中的sc实用程序安装服务时,无需提供凭据。

我是否必须为此服务创建登录凭据?我希望像其他服务一样使用“本地系统”或“网络服务”(不确定区别)。


3
在ProjectInstaller中,右键点击serviceProcessInstaller => 属性,将账户设置为LocalSystem。 - Majid Azarniush
5个回答

114

将以下代码添加到您的Windows服务项目中projectInstaller.Designer.cs文件中的私有void InitializeComponent()方法中。

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

如果您的进程安装程序的定义是:

private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;

@anthares,我需要使用当前用户来运行服务,我该如何解决?我尝试使用ServiceAccount.User,但在安装时会出现密码请求的问题... - Zhenya
4
由于该代码位于设计师生成的代码中,我只需打开设计师,获取serviceProcessInstaller1对象的属性,然后在“帐户”下拉列表中选择LocalSystem以执行相同操作。 - Mike K
我更喜欢下面@David的答案,使用设计师...... 大体上做同样的事情,但我认为那种方法更好。 - mike
and remember to rebuild - West

24

请查看此链接:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

请注意这个部分:创建服务的安装程序

对 ServiceProcessInstaller 进行更改:

在设计器中,对于 Visual Basic 项目,请单击 ServiceProcessInstaller1,对于 Visual C# 项目,请单击 serviceProcessInstaller1。将 Account 属性设置为 LocalSystem。这将导致服务被安装并在本地服务帐户上运行。


1
这比通过在projectInstaller.Designer.cs文件中设置代码优先级更高。 - Jerther

4
在包含服务的项目中添加一个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");
        }
    }
}

然后,在解决方案资源管理器中,右键单击部署项目并选择“查看 > 自定义操作”。右键单击自定义操作,选择“添加自定义操作...” 选择应用程序文件夹并选择包含服务的项目的主输出。现在,安装时将执行自定义操作(来自上面的 Commit)。如果您需要其他自定义操作,可以添加其他方法(InstallRollbackUninstall)。


我不明白,将系统账户设置为服务的执行者,如何帮助他? - anthares
这就是我从我的项目中复制代码的结果。{facepalm} 在我的项目安装程序的Install方法中,我设置了“Account”属性。现在已经修复了... - fre0n

4

1
我在 Misc 下面找不到 Account 下拉菜单,使用的是 VS 2010。 - Zabaa
1
他的意思是ServiceProcessInstaller,而不是ProcessInstaller。 - Zoomzoom

0
最简单的方法是点击您的serviceProjectInstaller并在属性窗口中进行设置。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接