Skip to content

Commit

Permalink
[fix]外部传入的args来自Main方法,不包含dll参数
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 24, 2024
1 parent bd68ff1 commit 5dee627
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions NewLife.Agent/Command/CommandHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CommandFactory(ServiceBase service, params Assembly[] customCommandHandle
/// </summary>
/// <param name="cmd">命令</param>
/// <param name="args">参数</param>
public Boolean Handle(String cmd, String[] args = null)
public Boolean Handle(String cmd, String[] args)
{
if (_commandHandlerDict.TryGetValue(cmd, out var handler))
{
Expand All @@ -84,7 +84,7 @@ public Boolean Handle(String cmd, String[] args = null)
/// </summary>
/// <param name="key"></param>
/// <param name="args"></param>
public Boolean Handle(Char key, String[] args = null)
public Boolean Handle(Char key, String[] args)
{
foreach (var commandHandler in _commandHandlerList)
{
Expand Down
14 changes: 8 additions & 6 deletions NewLife.Agent/CommandHandler/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions NewLife.Agent/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override void Dispose(Boolean disposing)
/// <param name="args"></param>
public void Main(String[] args)
{
args ??= Environment.GetCommandLineArgs();
if (args == null || args.Length == 0) args = Environment.GetCommandLineArgs();

if ("-Autorun".EqualIgnoreCase(args)) UseAutorun = true;

Expand All @@ -106,6 +106,7 @@ public void Main(String[] args)
if (!DisplayName.IsNullOrEmpty()) Console.Title = DisplayName;

Command.Handle(CommandConst.ShowStatus, args);

// 输出状态,菜单循环
ProcessMenu(args);
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions UnitTest/InstallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
}
}

0 comments on commit 5dee627

Please sign in to comment.