From 5dee627cd8d23f2bd07c2913d8a129b6cf7611e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 24 Dec 2024 16:56:08 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E5=A4=96=E9=83=A8=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E7=9A=84args=E6=9D=A5=E8=87=AAMain=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=8C=85=E5=90=ABdll=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Agent/Command/CommandHandlerFactory.cs | 4 ++-- NewLife.Agent/CommandHandler/Install.cs | 14 ++++++++------ NewLife.Agent/ServiceBase.cs | 5 +++-- UnitTest/InstallTests.cs | 7 ++++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/NewLife.Agent/Command/CommandHandlerFactory.cs b/NewLife.Agent/Command/CommandHandlerFactory.cs index dc75b08..b5fff6e 100644 --- a/NewLife.Agent/Command/CommandHandlerFactory.cs +++ b/NewLife.Agent/Command/CommandHandlerFactory.cs @@ -69,7 +69,7 @@ public CommandFactory(ServiceBase service, params Assembly[] customCommandHandle /// /// 命令 /// 参数 - public Boolean Handle(String cmd, String[] args = null) + public Boolean Handle(String cmd, String[] args) { if (_commandHandlerDict.TryGetValue(cmd, out var handler)) { @@ -84,7 +84,7 @@ public Boolean Handle(String cmd, String[] args = null) /// /// /// - public Boolean Handle(Char key, String[] args = null) + public Boolean Handle(Char key, String[] args) { foreach (var commandHandler in _commandHandlerList) { diff --git a/NewLife.Agent/CommandHandler/Install.cs b/NewLife.Agent/CommandHandler/Install.cs index fd14e5c..0f0e50b 100644 --- a/NewLife.Agent/CommandHandler/Install.cs +++ b/NewLife.Agent/CommandHandler/Install.cs @@ -28,16 +28,18 @@ public override void Process(String[] args) var exe = GetExeName(); // 兼容dotnet - if (args.Length == 0) - { - args = Environment.GetCommandLineArgs(); - } - if (args.Length >= 1) + //if (args == null || args.Length == 0) args = Environment.GetCommandLineArgs(); + // 外部传入的args来自Main方法,不包含dll参数。 + // 参考:https://newlifex.com/core/command_line_args + // 参考:https://newlifex.com/tech/dotnet_args + var args2 = Environment.GetCommandLineArgs(); + if (args == null || args.Length == 0) args = args2; + if (args2 != null && args2.Length >= 1) { var fileName = Path.GetFileName(exe); if (exe.Contains(' ')) exe = $"\"{exe}\""; - var dll = args[0].GetFullPath(); + var dll = args2[0].GetFullPath(); if (!dll.Contains(".dll"))//没有获得到主程的dll { dll = Environment.CommandLine?.Split(' ')[0];//Assembly.GetExecutingAssembly().Location; diff --git a/NewLife.Agent/ServiceBase.cs b/NewLife.Agent/ServiceBase.cs index 8b92f26..a749135 100644 --- a/NewLife.Agent/ServiceBase.cs +++ b/NewLife.Agent/ServiceBase.cs @@ -83,7 +83,7 @@ protected override void Dispose(Boolean disposing) /// public void Main(String[] args) { - args ??= Environment.GetCommandLineArgs(); + if (args == null || args.Length == 0) args = Environment.GetCommandLineArgs(); if ("-Autorun".EqualIgnoreCase(args)) UseAutorun = true; @@ -106,6 +106,7 @@ public void Main(String[] args) if (!DisplayName.IsNullOrEmpty()) Console.Title = DisplayName; Command.Handle(CommandConst.ShowStatus, args); + // 输出状态,菜单循环 ProcessMenu(args); } @@ -423,7 +424,7 @@ protected virtual void DoCheck(Object data) if (CheckAutoRestart()) return; // 检查看门狗 - Command.Handle(CommandConst.WatchDog); + Command.Handle(CommandConst.WatchDog, null); } private DateTime _nextCollect; diff --git a/UnitTest/InstallTests.cs b/UnitTest/InstallTests.cs index 937c662..54c1863 100644 --- a/UnitTest/InstallTests.cs +++ b/UnitTest/InstallTests.cs @@ -30,7 +30,7 @@ public void InstallExe() Assert.NotNull(model); Assert.Equal(svc.ServiceName, model.ServiceName); Assert.Equal(svc.DisplayName, model.DisplayName); - Assert.Equal("-s", model.Arguments); + Assert.StartsWith("-s", model.Arguments); } [Fact] @@ -56,13 +56,14 @@ public void InstallDll() Assert.NotNull(model); Assert.Equal(svc.ServiceName, model.ServiceName); Assert.Equal(svc.DisplayName, model.DisplayName); - Assert.Equal("-s", model.Arguments); + Assert.StartsWith("-s", model.Arguments); var cur = ".".GetFullPath(); Assert.Equal(cur, model.WorkingDirectory); var exe = cur.CombinePath("testhost.exe"); - var dll = cur.CombinePath("StarAgent.dll"); + //var dll = cur.CombinePath("StarAgent.dll"); + var dll = cur.CombinePath("testhost.dll"); Assert.Equal($"{exe} {dll}", model.FileName); } }