1

I'm trying to pass in parameters to my windows service but the args in OnStart method is always empty.

protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
        }

I have multiple instances of my service installed on the same machine. Each should use different config so the idea to get the service name and according to that to read the right config.

UshaP
  • 1,271
  • 2
  • 18
  • 32

3 Answers3

1

Are you passing the parameters through the service applet? Services can not accept command-line arguments and instead must be passed via the services applet in the "computer management" window.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • When I try to add in the "Start parameters" field i'm still getting empty arguments. – UshaP Nov 24 '10 at 21:48
  • Something has to be off, the start parameters translate to the args array you mentioned. Alternatively, I found an article on specifying those parameters in the installer: http://stackoverflow.com/questions/200163/am-i-running-as-a-service/2111492#2111492 – Brad Christie Nov 24 '10 at 21:56
0

If you want the service name, why not just look at ServiceBase.ServiceName?

protected override void OnStart(string[] args)
{
    switch (this.ServiceName)
    {
        // ....
    }
}

You might also want to consider a design not based around on the service name, which can be changed easily within the system.

  • that isn't good for me because the service name is always the same. i want to pass in parameter – UshaP Nov 24 '10 at 21:46
  • So how do you intend to have multiple services with the same name (which you can't do)? –  Nov 24 '10 at 21:47
  • I agree that the service name has to be unique but when i get this.ServiceName it always return service1 – UshaP Nov 24 '10 at 21:53
  • ctrl + shift + F and search your solution for Service1....then change it to whatever you want. – Ryan Eastabrook Nov 25 '10 at 00:26
0

Use a Settings file (Right click, Add Item, Settings) which will auto generate the app config file. Then in the bin for each instance, you can set the values in the config - IF you are running them out of different locations.

If it is multiple service instances running out of one location, the config file approach is not going to work. In that case, I would recommend a regular exe with a void main(string[] args) method and then you can use Task Scheduler in windows to define the arguments and the scheduling.

Josh
  • 16,286
  • 25
  • 113
  • 158