在自动启动时将参数传递给 Windows 服务 [英] Pass an argument to a Windows Service at automatic startup

查看:24
本文介绍了在自动启动时将参数传递给 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些类似的问题,但答案似乎对我没有帮助.我希望将我的服务配置为使用 1 个参数自动启动.

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.

我的服务 OnStart 方法如下所示:

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!
        }
    }

因此,我在服务文件名后使用 int 参数 (8081) 注册了我的服务,如下面的屏幕截图所示(如对类似问题的其他答案所建议).

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.

如何让 Windows 使用一个参数 (8081) 自动启动我的服务?

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

我又做了一些测试.添加了 args[] 参数的日志记录.这是空的.我也尝试添加额外的参数,如这张图片:

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.

推荐答案

当服务启动时,有两个不同的参数列表.

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

第一个取自命令行,如服务管理工具中的可执行文件路径"所示.这是您放置参数 8081 的位置,如屏幕截图所示.

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.

在 .NET 服务中,这些参数被传递给 Main() 函数.

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.

在 .NET 服务中,这些参数被传递给 OnStart() 函数.

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

因此,在您的方案中,您应该修改 Main() 以便它将命令行参数传递给您的服务类.通常您会在构造函数中提供这些,但如果您愿意,也可以使用全局变量.

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.)

这篇关于在自动启动时将参数传递给 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆