PDA

Click to See Complete Forum and Search --> : best way to pass info to a service


jpritchard
Dec 2nd, 2005, 06:27 AM
Hi,

I have written a service in c# and have go it working to the best of my ability.

Within my code I have a load of things that are possibly changeable in the future, so rather than hardcode within the program I have held this information within an xml "config" file. I can just change this file and restart the service and off it goes using the new parameters (I load the file on the OnStart event and load the data into the right fields within...etc.)

I have called my file config.xml and when I install the service, the home directory for it is windows\system32 - I therefore have to place my file here (I can hardcode it to say c:\ or some other dir)

what I would like is full flexability to specify the location, Ideally doing something like this

net start mydotnetservice -config_file=c:\mydir\config.xml

I would like to pass into the service the location of its config file to use
I have tried the following

on the Main() i tried adding this string[] args

eg static void Main(string[] args)

then using args[0].ToString();but nothing

also have again tried OnStart(string[] args)

but again args[0].ToString(); is null.

is it possible to pass anything into a service using the net start command. otherwise I'm forced to hold my file in a known location or so something like use a know location in the reg that points to the file (somthing im not keen on)

any thoughts appreciated,
cheers AJP

mendhak
Dec 2nd, 2005, 10:46 AM
You could modify your code to use a custom XML file with your values in it. It'd use an XMLReader for example. This'd allow you to pass in the parameters such as location of the file.

Granted, it'd take a considerable amount of extra coding to get it done.

conipto
Dec 2nd, 2005, 01:29 PM
Hmm, I'm not sure with a service, but with a standard app, you can call Environment.GetCommandLineArgs() which returns a string array of the command line parameters.

Bill

jpritchard
Dec 5th, 2005, 07:30 AM
hi

thanks for the comments

I used Environment.GetCommandLineArgs() to dump the command line and got

"c:\...etc..\windowsservice1\bin\debug\windowsservice1.exe"

which is what I guess is eventually happening with me "net start"ing it.
I think that I am asking for something not do-able - them program net.exe would have to take any parameter and pass this thru to the actual service program (as a parmeter list) and is just not designed to do that.

thanks anyway,
cheers AJP

umilmi81
Dec 6th, 2005, 09:16 PM
I don't think you can pass parameters in. But you can read your configuration file from the application directory instead of a hard coded directory.

There IS a way to do advanced communication with services, but you are going to have to use Remoting. That's beyond my knowledge at this point however.

conipto
Dec 6th, 2005, 09:53 PM
If you go into the services Console, you can open a services properties, and if it is stopped, specify commandline parameters. Can you see them with Environment.GetCommandLineArgs that way?

If this is the case, then you should be able to configure an installer to set up the service with your needed parameters, but I'm not sure how to go about it if they need to change frequently.

Bill