3

I have successfully passed parameters from Installutil to my serviceinstaller but i cannt seem to pass these parameters to the Main(string[ args] function. Here is how i am trying to do this ....if there is any better wayto do what i am doing please let me know

    protected override void OnAfterInstall(IDictionary savedState)
    {
        base.OnAfterInstall(savedState);
        string[] args = new string[2];
        args[0] = Context.Parameters["username"];
        args[0] = Context.Parameters["password"];
        new ServiceController(this.dataLoaderServiceInstaller.ServiceName).Start(args);
    }

and this is my Program.cs

    static void Main(string[] args)
    {
        // create a writer and open the file
        TextWriter tw = new StreamWriter(@"c:\\bw\\date.txt");

        // write a line of text to the file
        tw.WriteLine(args.Length);

        // close the stream
        tw.Close();
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
    { 
      new DataloaderService() 
    };
        ServiceBase.Run(ServicesToRun);
    }

the length of arugments that i am trying to write is always zero. One more question will these parameters still exist after the computer/server restarts for maintenance? Thanks in advance. :)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Zahid
  • 1,822
  • 4
  • 18
  • 26

1 Answers1

2

Parameters you send using ServiceController.Start() method are available to you as parameters to the OnStart() method. If I'm not mistaken (It's been a while since I needed to do this).

The OnStart method's signature is:

OnStart(string[] args)

However, if you need the parameters to be sent to the service each time the service is started (automatically) on boot, then you should look at the MSDN documentation on this. Specifically

Process initialization arguments for the service in the OnStart method, not in the Main method. The arguments in the args parameter array can be set manually in the properties window for the service in the Services console. The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx

Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
  • thanks Kumar :) what would be best way to store a password ...ImagePath doesn't look like the best way to store a password – Zahid Feb 20 '11 at 17:36
  • I don't believe services should need passwords and such. If you need the service to run as a specific user then that can be set up after install. So maybe a rethinking of your design is in order – Shiv Kumar Feb 20 '11 at 21:56
  • my service access email account for some processing so i need to store password for my email account – Zahid Feb 21 '11 at 17:01
  • Then use the registry as described in the MSDN documentation. You could also use a file (like a config file) that is in a known location that contains the information. In either case the password can be encrypted if you're concerned about that. – Shiv Kumar Feb 21 '11 at 17:35
  • the msdn documentation on the imagepath is a little hazy. can you provide a sample that does it? – Aviad Rozenhek May 17 '11 at 14:45