Pass argument to Windows service when starting automatically

I found some similar questions, but the answers do not seem to help in my case. I want to configure my service to start automatically with 1 argument.

My OnStart service method is as follows:

/// <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, as 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 running ...".

If I enter an integer in the "Start Parameters:" field, the service starts normally.

How can I automatically start Windows with a single argument (8081)?

Edit:

I did some more tests. Added args [] parameter log. it's empty. I also tried to add additional parameters, as in this image:

enter image description here

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

+1
source share
2 answers

When the service starts, there are two different argument lists.

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

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

The second is the service start options that are provided when the service starts manually. If you use the "Service" utility tool to start the service, this argument list is taken from the "start parameters" field, which is empty in your screenshot.

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

Therefore, in your script, you must modify Main() so that it passes command-line arguments to the service class. You usually provide them in the constructor, although you can use a global variable if you want.

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

+3
source
Answer to

@Harry Johnston is correct. I just want to add some code to support it.

The service entry point is in the "Program.cs" file. By default, it looks like this:

 static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service() }; ServiceBase.Run(ServicesToRun); } } 

No parameters are passed to the service. Adding the args parameter allows the service to receive arguments.

 static class Program { static void Main(string[] args) { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service(args[0]) }; ServiceBase.Run(ServicesToRun); } } 

Then you just need to add the constructor to the service accepting the parameter.

And now the service can be started automatically using the parameter.

+2
source

Source: https://habr.com/ru/post/1264077/


All Articles