Search code examples
asp.net-web-apiowinself-hostingtopshelf

Why custom command line arguments do not work when TopShelf service is installed as Win Service?


They work perfectly as console application. This is what I do:

   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;
    }

As console application it is printed in trace-log that all these are ok. If I install it (all custom arguments in command) and start it (all custom arguments in command) from command line they are empty in trace-log.


Solution

  • As I asked this question in a stupid way, the correct answer can be only that it is not supported in Topshelf.