TopShelf Running As Service Doesn't Pass The Arguments

36 Views Asked by At

When running the code below using Powershell/cmd

DataReplicator.exe run -h:localhost -d:networkdatabase -U:root -pwd:testpwd -tbl:testtable -dbms:1 -p:3306.

Its running as expected but when I run this as a service, the arguments are not passed to my constructor but on their default values. What am I doing wrong here? Do I need to do more? The code below:

static void Main(string[] args)
{
    var exitCode = HostFactory.Run(x =>
    {
        x.AddCommandLineDefinition("h", v => hostName = v); 
        x.AddCommandLineDefinition("p", v => port = Int32.Parse(v));                                                                             
        x.AddCommandLineDefinition("U", v => user = v);
        x.AddCommandLineDefinition("pwd", v => password = v);
        x.AddCommandLineDefinition("d", v => dbName = v);
        x.AddCommandLineDefinition("tbl", v => tableName = v);
        x.AddCommandLineDefinition("dbms", v => dbType = Int32.Parse(v));               

        x.Service<DataReplicator>(r =>
        {
            r.ConstructUsing(replicate => new DataReplicator(hostName, dbName, user, password, port, tableName, dbType));
            r.WhenStarted(replicate => replicate.Start());
            r.WhenStopped(replicate => replicate.Stop());
        });

        x.RunAsLocalSystem();

        x.SetServiceName("TopShelfDataReplicator");
        x.SetDisplayName("TopShelf Data Replicator");
        x.SetDescription("Replicate the local data to the network");
        x.StartAutomatically();
    });

    int exitValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
    Environment.ExitCode = exitValue;
}
0

There are 0 best solutions below