当TopShelf服务作为Win Service安装时,为什么自定义命令行参数不起作用?

时间:2014-11-20 12:02:43

标签: asp.net-web-api owin self-hosting topshelf

它们完美地用作控制台应用程序。这就是我的工作:

   private static int Main()
    {

        string databaseName = null;
        string databaseUser = null;
        string databasePwd = null;
        string port = null;
        string logDirectory = null;
        string strLogLevel = null;

        var exitCode = HostFactory.Run(configurator =>
        {

            configurator.AddCommandLineDefinition("dbname", d => { databaseName = d; });
            configurator.AddCommandLineDefinition("dbuser", d => { databaseUser = d; });
            configurator.AddCommandLineDefinition("dbpassword", d => { databasePwd = d; });
            configurator.AddCommandLineDefinition("port", p => { port = p; });
            configurator.AddCommandLineDefinition("logdirectory", l => { logDirectory = l; });
            configurator.AddCommandLineDefinition("loglevel", l => { strLogLevel = l; });
            configurator.ApplyCommandLine();

            int intPort = 7909;
            if (port != null)
                intPort = Convert.ToInt32(port);

            SystemDataApplication.LogLevel logLevel = SystemDataApplication.LogLevel.Info;
            if (strLogLevel != null)
                logLevel = (SystemDataApplication.LogLevel)Convert.ToInt32(strLogLevel);

            configurator.Service<SystemDataApplication>(service =>
            {
                service.ConstructUsing(() => new SystemDataApplication(databaseName, databaseUser, databasePwd, intPort, logDirectory, logLevel));
                service.WhenStarted(a => a.Start());
                service.WhenStopped(a => a.Stop());
            });

            configurator.SetDescription("An application to fetch system data.");
            configurator.SetDisplayName("System Data Service");
            configurator.SetServiceName("SystemDataService");

            configurator.RunAsNetworkService();
        });

        return (int)exitCode;
    }

作为控制台应用程序,它在trace-log中打印,所有这些都可以。如果我安装它(命令中的所有自定义参数)并从命令行启动它(命令行中的所有自定义参数),则它们在trace-log中为空。

1 个答案:

答案 0 :(得分:0)

当我以愚蠢的方式提出这个问题时,正确答案可能只是Topshelf不支持。