Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows service access 'start parameters' from Topshelf

I have added command line support to my topshelf program as follows:

HostFactory.Run(hostConfigurator =>
{
    hostConfigurator.AddCommandLineDefinition("params", f => { startParams = f; });    
    hostConfigurator.ApplyCommandLine(); 
}

And this works just fine.

When I install it as a service I was hoping in the installed service 'start parameters' it would serve the same purpose but it doesn't.

Can anyone tell me how to access the 'start parameters' from TopShelf?

I wish to install the same service multiple times (with different instance names) which are different by the start parameters and I also want to use it to pass in testing values.

I guess just simply accessing these programmatically for a standard service will probably point me in the right direction.

thanks.

like image 421
Neil Walker Avatar asked Dec 16 '25 17:12

Neil Walker


1 Answers

Parameters related to Service installation, such as servicename, description, instancename etc. can be accessed as follows

HostFactory.Run(x =>
{
    x.Service((ServiceConfigurator<MyService> s) =>
    {
        s.ConstructUsing(settings =>
        {
            var instanceName= settings.InstanceName;
                return new MyService();
        });
    }
}

Or if your MyService implements ServiceControl

        HostFactory.Run(x =>
        {
            x.Service<MyService>((s) =>
            {
                var instanceName= s.InstanceName;

                return new MyService();
            });
         }
/***************************/

class MyService : ServiceControl
{
    public bool Start(HostControl hostControl) {  }

    public bool Stop(HostControl hostControl)  {  }
}

I'm not sure what you mean by "Start Parameters", if above is not what you want, try to explain with pseudo-code example what you are trying to achieve.

like image 85
Sergej Popov Avatar answered Dec 19 '25 13:12

Sergej Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!