如何使用不是由TopShelf识别命令行参数?是由、如何使用、命令行、参数

2023-09-03 04:45:08 作者:我媳妇长得漂亮 你别碰

我想通过一些自定义参数的控制台应用程序,当我安装并启动它通过TopShelf Windows服务。

I want to pass some custom arguments to the console app when I install and start it as a Windows Service via TopShelf.

当我使用:

MyService install start /fooBar: Test

控制台应用程序失败:

Console application fails:

[失效]命令行一个未知的命令行选项,发现:   DEFINE:Foobar的=测试

[Failure] Command Line An unknown command-line option was found: DEFINE: fooBar = Test

问:

我怎样才能让我的论点是识别的TopShelf,这样我可以消耗他们的价值观?

How can I make my arguments to be recognizable by TopShelf so that I can consume their values?

推荐答案

根据需要指定在这个模式的命令的文档:

According to the documentation you need to specify the commands in this pattern:

-foobar:Test

您还需要添加在您的服务配置的定义:

You also need to add the definition in your service configuration:

string fooBar = null;

HostFactory.Run(configurator =>
{
    configurator.AddCommandLineDefinition("fooBar", f=> { fooBar = f; });
    configurator.ApplyCommandLine();

    // do something with fooBar

    configurator.Service<ServiceClass>(settings =>
    {
        settings.ConstructUsing(s => GetInstance<ServiceClass>());
        settings.WhenStarted(s => s.Start());
        settings.WhenStopped(s => s.Stop());
    });

    configurator.RunAsLocalService();
    configurator.SetServiceName("ServiceName");
    configurator.SetDisplayName("DisplayName");
    configurator.SetDescription("Description");
    configurator.StartAutomatically();
});