我想在OnStart期间将2个端口作为参数传递到Windows服务中。我已经看过几个例子并且遇到了这个指南C# Windows Service Startup Arguments。传递参数时此代码正常工作,但当我尝试传递两个参数时,出现错误Start Service fail 1053
。代码如下。
namespace MyService
{
static class Program
{
static void Main(string[] args)
{
uint port = uint.Parse(args[1]);
uint port2 = uint.Parse(args[2]);
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService(port,port2);
};
ServiceBase.Run(ServicesToRun);
}
}
}
以下代码是我的服务代码。
namespace MyService
{
public partial class MyService : ServiceBase
{
public uint Port { get; set; }
public uint Port2 { get; set; }
public MyService(uint port, uint port2)
{
InitializeComponent();
Port = port;
Port2 = port2;
}
protected override void OnStart(string[] args)
{
if(args.Length > 0) {
Port = uint.Parse(args[1]);
Port2 = uint.Parse(args[2]);
}
// DO STUFF
}
}
}
我使用以下命令安装它。
sc create WindowsService1 binPath= "C:\Users\user\Desktop\abc\Projects\WindowsService1\bin\x86\Debug\WindowsService1.exe --port 3000 --port 2000"
并开始使用以下命令
sc start WindowsService1 --port 3000 --port 2000