我做了一个topshelf windows服务,启动了三项任务。但是,由于可能会发生其中一个任务可能崩溃(是的,我知道EnableServiceRecovery
),最好使用一个程序创建3个服务使用不同的名称并安装它们使用命令行参数。
所以理论上代码看起来像是:
static void Main(string[] args)
{
// *********************Below is a TopShelf code*****************************//
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<MyService>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(() => new MyService(args[0])); //what service we are using
serviceConfigurator.WhenStarted(myService => myService.Start()); //what to run on start
serviceConfigurator.WhenStopped(myService => myService.Stop()); // and on stop
});
hostConfigurator.RunAsLocalSystem();
//****************Change those names for other services*******************************************//
hostConfigurator.SetDisplayName("CallForwardService"+args[0]);
hostConfigurator.SetDescription("CallForward using Topshelf"+args[0]);
hostConfigurator.SetServiceName("CallForwardService"+args[0]);
hostConfigurator.SetInstanceName(args[0]);
});
}
但当然它不会,因为(从我读过的内容)你不能简单地使用args[]
,但显然你可以使用像
Callforward.exe install --servicename:CallForward --instancename:Workshop
我仍然不确定如何传递稍后在程序中使用的参数(在上面的示例中,您可以在new MyService(args[0])
中看到它) - 这将是我的问题编号1。问题二(如标题中所示)将是&#34;我可以使用单个参数来设置所有三个元素(名称,实例和内部使用)吗?
编辑: 使用this answer
的帮助解决了问题string department = null;
// *********************Below is a TopShelf code*****************************//
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.AddCommandLineDefinition("department", f => { department = f; });
hostConfigurator.ApplyCommandLine();
hostConfigurator.Service<MyService>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(() => new MyService(department)); //what service we are using
serviceConfigurator.WhenStarted(myService => myService.Start()); //what to run on start
serviceConfigurator.WhenStopped(myService => myService.Stop()); // and on stop
});
hostConfigurator.EnableServiceRecovery(r => //What to do when service crashes
{
r.RestartService(0); //First, second and consecutive times
r.RestartService(1);
r.RestartService(1);
r.SetResetPeriod(1); //Reset counter after 1 day
});
hostConfigurator.RunAsLocalSystem();
//****************Change those names for other services*******************************************//
string d = "CallForwardService_" + department;
hostConfigurator.SetDisplayName(d);
hostConfigurator.SetDescription("CallForward using Topshelf");
hostConfigurator.SetServiceName(d);
});
我猜有时候我必须在论坛上发帖,然后再次阅读它们以完全理解我自己的问题。