Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an argument to a Windows Service at automatic startup

I found some similar questions, yet the answers doesn't seem to help in my case. I wish to configure my service for automatic startup with 1 argument.

My service OnStart method looks like this:

    /// <summary>
    /// Starts the service
    /// </summary>
    /// <param name="args">args must contain the listening port number of the service</param>
    protected override void OnStart(string[] args)
    {
        if (args != null && args.Length > 0)
        {
            int port = -1;
            if (int.TryParse(args[0], out port) && port >= 0 && port <= 65535)
            {
                server.Start(port);
            }
            else
            {
                Log.Entry("Port value " + args[0] + " is not a valid port number!", Log.Level.Error);
            }
        }
        else
        {
            Log.Entry("Service must be started with the port number (integer) as parameter.", Log.Level.Error);
            throw new ArgumentNullException("Service must be started with the port number (integer) as parameter."); // stop the service!
        }
    }

So I registered my service with the int parameter (8081) after the service file name like in the screenshot below (as suggested in other answers to similar questions).

enter image description here

When I start the service, I always get the error message "The service must be started....".

If I type an integer in the "Start parameters:" field, the service starts normally.

How can I have Windows automatically start my service with one argument (8081)?

Edit:

I did some more tests. Added logging of the args[] parameter. It is empty. Also I tried to add extra parameters like in this image:

enter image description here

I tried both with and without double-quotes around the arguments, but they are not passed to the service.

like image 389
Mr. Blonde Avatar asked Oct 15 '25 01:10

Mr. Blonde


1 Answers

When a service is started, there are two distinct argument lists.

The first is taken from the command line, as shown in "path to executable" in the Services administrative tool. This is where you have put the argument 8081 as shown in the screenshot.

In a .NET service, these arguments are passed to the Main() function.

The second is the service start parameters, which are provided when the service is manually started. If you use the Services administrative tool to start the service, this argument list is taken from the "start parameters" field, which in your screenshot is empty.

In a .NET service, these arguments are passed to the OnStart() function.

In your scenario, therefore, you should modify Main() so that it passes the command-line arguments on to your service class. Typically you would provide these in the constructor, although you could use a global variable if you preferred.

(See also this answer for a more detailed description of a service startup.)

like image 102
Harry Johnston Avatar answered Oct 17 '25 15:10

Harry Johnston