用于控制Topshelf Windows服务的单个命令行参数

Yas*_*ier 6 c# service topshelf

我做了一个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]);
    });
}
Run Code Online (Sandbox Code Playgroud)

但当然它不会,因为(从我读过的)你不能简单地使用,args[]但显然你可以使用类似的东西

Callforward.exe install --servicename:CallForward --instancename:Workshop
Run Code Online (Sandbox Code Playgroud)

我仍然不确定如何传递稍后在程序中使用的参数(在上面的例子中你可以看到它new MyService(args[0])) - 这将是我的问题编号1.问题二(如标题中所示)将是"可以我使用单个参数来设置所有三个元素(名称,实例和内部使用)?

编辑:使用此答案的帮助解决

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);
});
Run Code Online (Sandbox Code Playgroud)

我想有时我必须在论坛上发布内容然后再次阅读它们以完全理解我自己的问题.

vin*_*zee 0

使用How can I use CommandLine Arguments that is not recognize by TopShelf? 的帮助解决了

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);
});
Run Code Online (Sandbox Code Playgroud)

此答案是由 OP Yasskier在 CC BY-SA 3.0 下对问题Single command line parameter to control Topshelf windows service 的编辑发布的。