问题
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).
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:
I tried both with and without double-quotes around the arguments, but they are not passed to the service.
回答1:
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.)
回答2:
@Harry Johnston's answer is correct. I just want to add a little code to support it.
The entry point of the service is located in the file "Program.cs". Per 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 simply need to add a constructor to the service accepting the parameter.
And now the service can be automatically started with a parameter.
来源:https://stackoverflow.com/questions/42812333/pass-an-argument-to-a-windows-service-at-automatic-startup