Results 1 to 6 of 6

Thread: best way to pass info to a service

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    280

    best way to pass info to a service

    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

    VB Code:
    1. 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
    VB Code:
    1. static void Main(string[] args)

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

    also have again tried
    VB Code:
    1. 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
    Intel 486dx3 - VB4 - DR-Dos - 2mb EdoRam - 2x CDrom drive - Window for Workgroups - CGA monitor

    Real programmers use less....

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: best way to pass info to a service

    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.

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: best way to pass info to a service

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    280

    Re: best way to pass info to a service

    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
    Intel 486dx3 - VB4 - DR-Dos - 2mb EdoRam - 2x CDrom drive - Window for Workgroups - CGA monitor

    Real programmers use less....

  5. #5
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: best way to pass info to a service

    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.

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: best way to pass info to a service

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width