我已成功将参数从 Installutil 传递到我的 serviceinstaller,但我似乎无法将这些参数传递到 Main(string[ args] 函数。
这就是我尝试做到这一点的方法...如果有更好的方法来做我正在做的事情请告诉我
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
string[] args = new string[2];
args[0] = Context.Parameters["username"];
args[0] = Context.Parameters["password"];
new ServiceController(this.dataLoaderServiceInstaller.ServiceName).Start(args);
}
这是我的 Program.cs
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter(@"c:\\bw\\date.txt");
// write a line of text to the file
tw.WriteLine(args.Length);
// close the stream
tw.Close();
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new DataloaderService()
};
ServiceBase.Run(ServicesToRun);
}
我试图写入的参数长度始终为零。
还有一个问题,电脑/服务器维护重启后这些参数还会存在吗?
提前致谢。 :)
使用 ServiceController.Start() 方法发送的参数可用作 OnStart() 方法的参数。如果我没记错的话(自从我需要这样做以来已经有一段时间了)。
OnStart 方法的签名是:
OnStart(string[] args)
但是,如果您需要在每次启动时(自动)启动服务时将参数发送到服务,那么您应该查看相关的 MSDN 文档。具体来说
处理初始化参数
OnStart 方法中的服务,而不是
在Main方法中。中的论据
可以设置args参数数组
在属性窗口中手动
服务控制台中的服务。
在控制台中输入的参数
没有得救;它们被传递给
一次性服务,当
服务从控件启动
控制板。必须存在的参数
当服务自动
启动后可以放在ImagePath中
服务的字符串值
注册表项
(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\)。您可以获得参数
从注册表中使用
GetCommandLineArgs 方法,用于
示例:字符串[] imagePathArgs =
环境.GetCommandLineArgs();。
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)