C# 使用InstallUtil安装带有启动参数的Windows服务
我正在使用InstallUtil来安装我的服务,我就是不知道如何为它指定启动参数 以下是我的安装程序子类:C# 使用InstallUtil安装带有启动参数的Windows服务,c#,.net,windows-services,installutil,C#,.net,Windows Services,Installutil,我正在使用InstallUtil来安装我的服务,我就是不知道如何为它指定启动参数 以下是我的安装程序子类: [RunInstaller(true)] public class ServerHostInstaller : Installer { private ServiceInstaller m_serviceInstaller; private ServiceProcessInstaller m_serviceProcessInstaller; private static str
[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;
}
}
[运行安装程序(true)]
公共类ServerHostInstaller:安装程序
{
私人服务安装商m_ServiceInstaller;
私有ServiceProcessInstaller m_ServiceProcessInstaller;
私有静态字符串s_usage=“用法:\ninstallutil/i/username=/password=NCStub.Server.Host.exe”;
公共服务器主机安装程序()
{
m_serviceInstaller=新serviceInstaller();
m_serviceInstaller.ServiceName=Program.ServiceName;
m_serviceInstaller.DisplayName=Program.ServiceName;
m_serviceInstaller.StartType=ServiceStartMode.Automatic;
m_serviceProcessInstaller=新的serviceProcessInstaller();
m_serviceProcessInstaller.Account=ServiceAccount.User;
安装程序。添加(m_serviceInstaller);
添加(m_serviceProcessInstaller);
}
公共覆盖无效安装(IDictionary stateSaver)
{
安装(stateSaver);
字符串userName=this.Context.Parameters[“userName”];
如果(用户名==null)
{
Console.WriteLine(s_用法);
抛出新的InstallException(“缺少参数‘username’”);
}
字符串userPass=this.Context.Parameters[“password”];
if(userPass==null)
{
Console.WriteLine(s_用法);
抛出新的InstallException(“缺少参数‘密码’”);
}
m_serviceProcessInstaller.Username=用户名;
m_serviceProcessInstaller.Password=userPass;
}
}
有人能指出我如何指定服务启动参数吗 找到了
我重写了安装方法,如下所示:
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),但代码很容易适应传递真正的命令行参数,只需使用相同的模式传递用户名和密码参数。我知道这是一篇老文章,但我想我会发布我的回复。我在.NET4服务中使用BeforeInstall事件实现了这一点 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();
}
}
已安装的服务如下所示:
它执行得很好,我可以从服务的主功能中检查参数。如果您将处理程序附加到服务安装程序对象的BeforeInstall事件,而不是覆盖Install方法,则这种方法也可以工作。实际上,不,它不能。应该是的,我很确定它曾经是,但我只是检查了一下,它没有。坚持使用覆盖版本。我有相同的解决方案将凭据传递给我的安装程序。问题是日志文件也包含您的凭据,这在我看来是一个大问题。你知道如何禁止在日志文件中写入“受影响的参数是:”吗?我不想禁用完整的日志文件@弗洛里安格哈特-不。从那时起,我开始使用Java和Javascript,在过去的半年里没有使用.NET。抱歉,伙计,这个解决方案不起作用。它为服务生成如下命令:“C:\folder\file.exe”--服务