使用InstallUtil安装与启动参数的Windows服务 [英] Using InstallUtil to install a Windows service with startup parameters
本文介绍了使用InstallUtil安装与启动参数的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用InstallUtil安装我的服务,我只是无法弄清楚如何为它指定启动参数
I am using InstallUtil to install my service and I just cannot figure out how to specify the startup parameters for it!
下面是我的安装程序子类:
Here is my Installer subclass:
[RunInstaller(true)]
public class ServerHostInstaller : Installer
{
private ServiceInstaller m_serviceInstaller;
private ServiceProcessInstaller m_serviceProcessInstaller;
private static string s_usage = "Usage:\ninstallutil /i /username=<user_name> /password=<user_password> NCStub.Server.Host.exe";
public ServerHostInstaller()
{
m_serviceInstaller = new ServiceInstaller();
m_serviceInstaller.ServiceName = Program.ServiceName;
m_serviceInstaller.DisplayName = Program.ServiceName;
m_serviceInstaller.StartType = ServiceStartMode.Automatic;
m_serviceProcessInstaller = new ServiceProcessInstaller();
m_serviceProcessInstaller.Account = ServiceAccount.User;
Installers.Add(m_serviceInstaller);
Installers.Add(m_serviceProcessInstaller);
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string userName = this.Context.Parameters["username"];
if (userName == null)
{
Console.WriteLine(s_usage);
throw new InstallException("Missing parameter 'username'");
}
string userPass = this.Context.Parameters["password"];
if (userPass == null)
{
Console.WriteLine(s_usage);
throw new InstallException("Missing parameter 'password'");
}
m_serviceProcessInstaller.Username = userName;
m_serviceProcessInstaller.Password = userPass;
}
}
任何人都可以表明我怎么指定服务启动参数?
Can anyone indicate how do I specify the service startup parameters?
推荐答案
找到了。
我已经改写像安装方法所以:
I have rewritten the Install method like so:
public override void Install(IDictionary stateSaver)
{
string userName = this.Context.Parameters["username"];
if (userName == null)
{
Console.WriteLine(s_usage);
throw new InstallException("Missing parameter 'username'");
}
string userPass = this.Context.Parameters["password"];
if (userPass == null)
{
Console.WriteLine(s_usage);
throw new InstallException("Missing parameter 'password'");
}
m_serviceProcessInstaller.Username = userName;
m_serviceProcessInstaller.Password = userPass;
var path = new StringBuilder(Context.Parameters["assemblypath"]);
if (path[0] != '"')
{
path.Insert(0, '"');
path.Append('"');
}
path.Append(" --service");
Context.Parameters["assemblypath"] = path.ToString();
base.Install(stateSaver);
}
虽然,我给预定义的命令行参数( - 服务),代码容易适应实际传递命令行参数,只需要使用相同的模式传递在用户名和<强>密码参数。
Although, I give the predefined command line parameters (--service), the code is easily adaptable to pass real command line arguments, just use the same pattern for passing the username and password parameters.
这篇关于使用InstallUtil安装与启动参数的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文