开发者

How to pass in arguments to Windows service

I'm trying to pass in parameters to my windows service but the args in OnStart method is always empty.

protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
        }

I have multiple instances of my service installed on 开发者_运维百科the same machine. Each should use different config so the idea to get the service name and according to that to read the right config.


Are you passing the parameters through the service applet? Services can not accept command-line arguments and instead must be passed via the services applet in the "computer management" window.


If you want the service name, why not just look at ServiceBase.ServiceName?

protected override void OnStart(string[] args)
{
    switch (this.ServiceName)
    {
        // ....
    }
}

You might also want to consider a design not based around on the service name, which can be changed easily within the system.


Use a Settings file (Right click, Add Item, Settings) which will auto generate the app config file. Then in the bin for each instance, you can set the values in the config - IF you are running them out of different locations.

If it is multiple service instances running out of one location, the config file approach is not going to work. In that case, I would recommend a regular exe with a void main(string[] args) method and then you can use Task Scheduler in windows to define the arguments and the scheduling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜