我正在使用InstallUtil来安装我的服务,我无法弄清楚如何为它指定启动参数!
这是我的安装程序子类:
[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;
}
}
任何人都可以说明如何指定服务启动参数?
答案 0 :(得分:15)
找到它。
我已经重写了Install方法,如下所示:
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);
}
虽然,我给出了预定义的命令行参数( - service ),但代码很容易适应传递真实的命令行参数,只需使用相同的模式传递用户名< / strong>和密码参数。
答案 1 :(得分:4)
我知道这是一个老帖子,但我想发布我的回复。我是在使用BeforeInstall事件的.net 4服务中完成的。
ServiceProcessInstaller的BeforeInstall事件:
private void serviceProcessInstaller1_BeforeInstall(object sender, InstallEventArgs e)
{
System.ServiceProcess.ServiceProcessInstaller installer = sender as System.ServiceProcess.ServiceProcessInstaller;
if (installer != null)
{
//Get the existing assembly path parameter
StringBuilder sbPathWIthParams = new StringBuilder(installer.Context.Parameters["assemblypath"]);
//Wrap the existing path in quotes if it isn't already
if (!sbPathWIthParams[0].Equals("\""))
{
sbPathWIthParams.Insert(0, "\"");
sbPathWIthParams.Append("\"");
}
//Add desired parameters
sbPathWIthParams.Append(" test");
//Set the new path
installer.Context.Parameters["assemblypath"] = sbPathWIthParams.ToString();
}
}
它执行正常,我可以从服务的main函数中检查参数。